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

LVL 12
Barry JonesAsked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
Have a look at this article.  It explains the ways to do it.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

http://www.laprbass.com/RAY_temp_thefoot.php
Outputs:
01/12/2011 = Wed, 12 Jan 2011 00:00:00 +0000
01-12-2011 = Thu, 01 Dec 2011 00:00:00 +0000

HTH, ~Ray
<?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";

// A DATE WITH DASHES IS IN 'UK' FORMAT
$d = '01-12-2011'; // 1st December 2011 UK format
$t = strtotime($d);
$x = date('r', $t);
echo PHP_EOL . "$d = $x";

Open in new window

0
 
Barry JonesAuthor Commented:
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

0
 
xtermConnect With a Mentor Commented:
This probably isn't terribly informative of a comment, but I've played with it, and what you have is pretty much ideal. I mean, you can explode() your input date on the "/" character, and flip your field order in that way, but preg_split works fine too.   Quite honestly, I'd roll what you have into a new function and call it a day.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Ray PaseurCommented:
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

0
 
Barry JonesAuthor Commented:
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
0
 
Ray PaseurConnect With a Mentor Commented:
It's not my idea that using slashes in a date is not a valid UK format -- it's just the way PHP works.  Some of that is explained tongue-in-cheek here:
http://www.addedbytes.com/blog/if-php-were-british/

See these links to get a feel for what PHP is doing with date strings.
http://www.laprbass.com/RAY_strtotime.php?s=1.12.2011
http://www.laprbass.com/RAY_strtotime.php?s=1-12-2011
http://www.laprbass.com/RAY_strtotime.php?s=1+12+2011
http://www.laprbass.com/RAY_strtotime.php?s=1/12/2011
0
 
Barry JonesAuthor Commented:
Ah I see.. sorry :-)

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

Cheers

TheFoot
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.