?
Solved

How to reliably convert UK date string to timestamp regardless of server default formatting?

Posted on 2011-10-25
7
Medium Priority
?
299 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:Barry Jones
  • 3
  • 3
7 Comments
 
LVL 12

Author Comment

by:Barry Jones
ID: 37025704
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
 
LVL 19

Assisted Solution

by:xterm
xterm earned 200 total points
ID: 37026775
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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1800 total points
ID: 37027017
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37027038
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
 
LVL 12

Author Comment

by:Barry Jones
ID: 37027278
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
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1800 total points
ID: 37027657
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
 
LVL 12

Author Comment

by:Barry Jones
ID: 37027672
Ah I see.. sorry :-)

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

Cheers

TheFoot
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question