Formatting date from number in database

Hey, I’ve had a look online and I can’t seem to get anything working that turns 13052011 back into 13th May 2011.

Stores it in database as date(“dmY”).
Would it be better to store it another format?

Hi there,

The best storage personally is in a UNIX timestamp. In a MySQL Query you can use “NOW()” as the value for a field or using php time() - both of which will return the UNIX timestamp as of when the script was called. When you then pull the timestamp out of the database for use in php, simply use date(“jS F Y”,$timestamp);

Thanks for replying.

I check to see if there is already an entry in the database based on the day of the year.
Doesn’t NOW() store the hours, minutes, seconds?

Sorry I only half explained! Yes in a DATETIME field it would return 2011-05-22 13:46:27 but in a TIMESTAMP field it would return 1306064787. If you aren’t adding it to a timestamp field you should simply be able to use the php function strtotime() when you pull the string out of the database

[php]echo strtotime(“2011-05-22 13:46:27”);//echoes 1306064787[/php]

I might be a bit lost now ???

So how do I store only the day month year in the database? I’m currently adding it to int field.

If the INT field is large enough (10 or more), I would personally store the UNIX timestamp as mentioned previously.

If not, then date(“Ymd”) - will output 20110522. Make sure that you stick to the same layout of year month and date (always same order and with trailing zeroes) to make converting it into a legible date easier, strtotime() will not recognise this input, so you will have to manually code PHP to split this up into the relevant segments of data.

Thanks so much for your help smokey.

I ended up just adding a new field in the database so I could still keep checking date(“dmY”) to see if it was the same day.

Then used NOW()+1 to store the exact time.

[php]
$this->FormatDate(“Y-m-d”, $data[$i][‘timestamp’])

function FormatDate($format, $time) {
if (strlen($time) < 4) {
return ‘N/A’;
}
$year=substr($time,0,4);
$month=substr($time,4,2);
$day=substr($time,6,2);
$hour=substr($time,8,2);
$minute=substr($time,10,2);
$seconds=substr($time,12,2);
$month = (strlen($month) > 1) ? $month1 : $month;
$day = (strlen($day) > 1) ? $day
1 : $day;
return date($format, mktime($hour,$minute,$seconds,$month,$day,$year));
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service