I have been developing on php 5.3.
However our production server is 5.2.6.
I have been using
$schedule = '31/03/2011 01:22 pm'; // example input
if (empty($schedule))
$schedule = date('Y-m-d H:i:s');
else {
$schedule = dateTime::createFromFormat('d/m/Y h:i a', $schedule);
$schedule = $schedule->format('Y-m-d H:i:s');
}
echo $schedule;
However that function is not available in 5.2
What is the easiest way to get around this (no chance of a php upgrade).
Date and Time only
ISO8601 no colons
ISO8601 with colons
Salesforce ISO8601 format
Hope this saves someone time!
I had the similar problem with the production server at 5.2, so I used the above datetime to create an object and then change the format to my liking as above.
just include the next code
I think it is much cleaner to extend the DateTime class and implement
createFromFormat()
yourself like this:-This will work in all versions of PHP >= 5.2.0.
See here for a demo http://3v4l.org/djucq
Since this is not really showing how to convert YYYY:DDD:HH:MM:SS time to unix seconds using the "z" option you have to create you own functions to convert the DOY to month and day of month. This is what I did:
The output on the page shows its working since I can convert back the seconds back to the original input values. unix seconds: 1483140203 12/30/2016 23:23:23
Because
strtotime
does poorly when confronted with D/M/Y anddate_create_from_format
isn't available,strptime
may be your only hope here. It does some pretty oldschool things, like deal with years as if they are the number of years since 1900 and deal with months as if January was month zero. Here's some horrible example code that usessprintf
to reassemble the date into something DateTime understands:If it works, you should see the same, correct date and time printed twice.