...

View Full Version : Converting a british date to a timestamp



jamestooks
12-09-2008, 09:13 PM
Hi,

At the moment I have some dates stored in my database as a string in british format (DD/MM/YYYY). I need to convert these dates into timestamps and then put them back into the database.

Converting them is easy enough using mktime() which turns this into a Unix Timestamp. Inserting that just puts in todays timestamp by default. I tried using the date() function to create a timestamp in mysql format (which I believe is YYYYMMDDhhmmss) but this didn't work either. If anybody could help me, that'd be awesome. Anyway, here's some of the code I was using to try and get this to work.


$date = explode("/",$articleResult['DateAdded']);

$newDate = mktime(0,0,0,$date[1],$date[0],$date[2]);

$newDate2 = date("YmdHis", $newDate);

mysql_query("UPDATE new_article SET DateAdded2 = '".$newDate2."' WHERE ArticleID = '".$articleResult['ArticleID']."'");

CFMaBiSmAd
12-09-2008, 10:02 PM
Use the mysql STR_TO_DATE() function directly in a single UPDATE query - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date

There is no need to select the data, loop through it, convert it, and UPDATE the DateAdded2 column. Just UPDATE the DateAdded2 column directly.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum