Link to home
Start Free TrialLog in
Avatar of Barry Jones
Barry JonesFlag for United Kingdom of Great Britain and Northern Ireland

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
// 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;

Open in new window

Avatar of Barry Jones
Barry Jones
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I've managed to get it to work by manually parsing the date string as ANSI-SQL standard date format.

Anyone with better solutions?

Thanks, TheFoot
$v_input_date = '18/12/2011'; // 18th December UK Format
$a_uk_format = list($d, $m, $y) = preg_split( '/[-\.\/ ]/', $v_input_date);
$v_ansi_format = $a_uk_format[2].'-'.$a_uk_format[1].'-'.$a_uk_format[0];
$v_ts = strtotime($v_ansi_format);
echo date('d/m/Y', $v_ts);
exit;

Open in new window

SOLUTION
Avatar of xterm
xterm

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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";

Open in new window

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah I see.. sorry :-)

Thanks very much for your informative comments.. I appreciate your time

Cheers

TheFoot