Barry Jones
asked on
How to reliably convert UK date string to timestamp regardless of server default formatting?
Hi All,
I have input dates always in UK format (dd/mm/yyyy).
I want to convert into a timestamp, but the server timezones are different between dev/staging and production servers.
I want to reliably convert a UK string date into a timestamp. Any ideas? The attached code does not work - I thought it would?
Thanks, TheFoot
I have input dates always in UK format (dd/mm/yyyy).
I want to convert into a timestamp, but the server timezones are different between dev/staging and production servers.
I want to reliably convert a UK string date into a timestamp. Any ideas? The attached code does not work - I thought it would?
Thanks, TheFoot
// Force UK date format
date_default_timezone_set('GMT'); // UTC? Europe/London?
$v_test_date = '01/12/2011'; // 1st December 2011 UK format
$o_date = new DateTime($v_test_date);
dump(array(
"Original (UK)" => $v_test_date,
"Formatted (Server)" => $o_date->format('d/m/Y')
));
exit;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
So to expand on that piece of code a little bit... The translator code is on line 14.
<?php // RAY_temp_thefoot.php
error_reporting(E_ALL);
echo "<pre>";
date_default_timezone_set('Europe/London');
// A DATE WITH SLASHES IS NOT IN 'UK' FORMAT
$d = '01/12/2011'; // 1st December 2011 NOT UK format
$t = strtotime($d);
$x = date('r', $t);
echo PHP_EOL . "$d = $x";
// CORRECT THE SEPARATOR CHARACTER TO MAKE A DATE WITH DASHES
$d = str_replace('/', '-', $d);
$t = strtotime($d);
$x = date('r', $t);
echo PHP_EOL . "$d = $x";
ASKER
Thanks for the link to the article Ray - very interesting info.
I'm not sure where you get the idea that using slashes in a date is not a valid UK format - my experience is that it is far more common in the UK than using dashes. Using preg-replace allows me to take into consideration both input formats which is a more flexible approach.
I (obviously) agree with your article that the best way to use dates server-side is to parse it to ansi/iso format - works all round php and mysql.
Thanks, TheFoot
I'm not sure where you get the idea that using slashes in a date is not a valid UK format - my experience is that it is far more common in the UK than using dashes. Using preg-replace allows me to take into consideration both input formats which is a more flexible approach.
I (obviously) agree with your article that the best way to use dates server-side is to parse it to ansi/iso format - works all round php and mysql.
Thanks, TheFoot
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ah I see.. sorry :-)
Thanks very much for your informative comments.. I appreciate your time
Cheers
TheFoot
Thanks very much for your informative comments.. I appreciate your time
Cheers
TheFoot
ASKER
Anyone with better solutions?
Thanks, TheFoot
Open in new window