I am using the following code to generate time stamps I always get different results for the same input on
$EventDateZone = new DateTime(str_replace('/', '-', $record['Date']), new DateTimeZone(Config::get('app.timezone')));
$EventDate = strtotime($EventDateZone->format("Y-m-d"));
$EventTimeZone = new DateTime($record['Time'], new DateTimeZone(Config::get('app.timezone')));
$EventTime = strtotime($EventTimeZone->format("H:i:s"));
The variables $record['Date]
includes 01/04/2014
and $record['Time']
includes 09:26:00 AM
and Config::get('app.timezone')
is Asia/Dubai
The problem with the converted time it is always different here are the result for the converted time 1398576360
and 1398662760
I need the generated time stamp to be identical as this record will be stored in the database and I don't want to have it duplicated.
The date value does not contain a time, and the time value does not contain a date. UNIX timestamps however are compound values which are date-time values and must contain both. When you instantiate a new DateTime
object and you give it an incomplete timestamp (i.e. missing either date or time), it fills in the missing values from the current time. And that will of course always be different.
You'll have to instantiate the DateTime
object with both to get your UNIX timestamp. You cannot output a UNIX timestamp separately for date and time, because that doesn't make any sense. Your strtotime
code doesn't make any sense. You can separate the DateTime
object into date and time components again later if necessary, but not into two separate UNIX timestamps.
$event = DateTime::createFromFormat(
'd/m/Y H:i:s',
"$record[Date] $record[Time]",
new DateTimeZone(Config::get('app.timezone'))
);
echo $event->getTimestamp();
echo $event->format('Y-m-d');
echo $event->format('H:i:s');