PHP time formatting confusion

This is actually a followup question From this question , but it presents a whole different problem . I also know it is pretty confusing so please bear with me ,.

I have a date that presents like : 1/04/2013 which is intended to be Jan 04 2013

Read More

In the previous mentioned question, I had to randomly add the hours and minutes to the time format , and it works for what it was supposed to do :

$clr_datenew = date('Y-m-d ' . sprintf('%02d:%02d:00', mt_rand(0, 23), mt_rand(0, 59)), strtotime($date));

this produced something like : 2013-05-01 08:57:00

The problem is , that now the system see that date as MAY 1st and not as JAN 05..

when I automatically insert those posts (wp) – all the posts that will have a date like 2012-05-28 will be inserted to the DB as 1970-01-01 ( I presume this is the default “wrong date” format for php .. 28th month is an error of course.. )

I tried to change the day month from

$clr_datenew = date('Y-m-d ' . sprintf('%02d:%02d:00', mt_rand(0, 23), mt_rand(0, 59)), strtotime($date));

to

$clr_datenew = date('Y-d-m ' . sprintf('%02d:%02d:00', mt_rand(0, 23), mt_rand(0, 59)), strtotime($date));

But it produces the same error with different date combinations .

After a colleague gave me a tip that :

strtotime interprets x/y/z as mm/dd/yy[yy], and x-y-z as dd-mm-yyyy or
yy-mm-dd, depending on whether z is 4 digits or 2.

I also tried :

date('Y-m-d ' . sprintf('%02d:%02d:00', mt_rand(0, 23), mt_rand(0, 59)), strtotime(str_replace('/','-',$clr_date)));

I am really loosing my mind here 🙂

I presume this is becasue I use strtotime() where the function actually “guess” how to format my time based on a loosely “human” format .

Is there a function to convert the dates from one date to another while specifying both the original date format and the wanted conversion format ??

something like dummy_function($origformat,$wantedformat) where I can specify dummy_function('Y-d-m','Y-m-d')

EDIT I
After reading the comment by @harke something triggered in me and combining it with the answer by @Maks3w it appears that the format which is working for me is :

$clr_datenew = date('Y-m-d ' . sprintf('%02d:%02d:00', mt_rand(0, 23), mt_rand(0, 59)), strtotime(str_replace('/','-',$clr_date)));

I could not verify the Php 5.3+ methods since they are not so relevant for me as the ( internal) server in that particular company is running lower version ..

A little extra something that should be posted on every billboard of DB designers / programmers : http://xkcd.com/1179/

That is – until further notice 🙂

Related posts

Leave a Reply

3 comments

  1. The first argument of date() is a format mask for print literals you must escape each char with a backslash

    $clr_datenew = date('Y-m-d ' . preg_replace('/(.)/', '\$1', sprintf('%d%d:%d%d:00', mt_rand(0, 23), mt_rand(0, 59))), strtotime($date));
    
    1. Decide on one internal time representation and stick to it. Preferably choose one that is a standard across many systems. m/d/y is not such a standard (despite what many Americans like to think ;P). Virtually the universal standard is Y-m-d H:i:s (expressed as format string for the date() function). Most databases expect this format, all time related functions can parse that format without ambiguity.

    2. The above is a useful text representation of the date that is interchangeable between different systems, but internally it makes sense to represent the date in an even more flexible type: a UNIX timestamp or a DateTime object. Pick one or the other and keep all your dates internally in your code in that format. Both allow easy manipulation of time values. Only change it back to a string when you need to display the date to a user or you need to send it as a string to another system (i.e. insert into a database). Don’t convert back and forth between m/d/y strings to UNIX timestamps to d/m/y strings.

    3. To parse a date in a known format into a UNIX timestamp or DateTime object to format it into something else later, use DateTime::createFromFormat on recent PHP versions (5.3+) or the older strptime followed by some manual reassembly.