tags:

views:

25

answers:

2

Hi,

I'm trying to a date timestampted in my mysql db, but my code shows the current date instead.

This is the code that I'm trying:

$db_date = $row_news['nm_date'];
$year = substr($db_date, 0, 4);
$mon = substr($db_date, 4, 2);
$day = substr($db_date, 6, 2);
$orgdate = date("l dS F Y",mktime($mon, $day, $year));
$date = $orgdate

The script is meant to email $date, which should be the value of $row_news['nm_date'], but instead I get the current date "Saturday 11th September 2010.

Thanks for your help.

A: 
$db_date = $row_news['nm_date'];
$orgdate = date("l dS F Y", $db_date);
$date = $orgdate;
Alexander.Plutov
$date = date("l dS F Y", $row_news['nm_date'];?
halfdan
Thanks for your help Alexander. But now I get : Thursday 01st January 1970
drhoo
Same with halfdan's code, I get "Thursday 01st January 1970 ". Thanks again
drhoo
+1  A: 

You're getting today's date because mktime() is not returning a valid value. If you check the manual entry for mktime() - http://www.php.net/mktime - you'll see that the parameter order is:

mktime ($hour, $minute, $second, $month, $day, $year)

so you probably want:

$orgdate = date("l dS F Y", mktime(12, 0, 0, $mon, $day, $year));

your code assumes that the date is in YYYYMMDD format (or YYYYMMDDHHIISS). Assuming that is correct, and it's not actually in date format (YYYY-MM-DD) then the above should fix your problem.

Edit: If the dates are in YYYY-MM-DD format you need to adjust your substrings to allow for the dashes:

$year = substr($db_date, 0, 4);
$mon = substr($db_date, 5, 2);
$day = substr($db_date, 8, 2);
Tim Fountain
Tim, your code outputs "Wednesday 09th December 2009 ". thanks
drhoo
Dates in DB are in YYYY-MM-DD format
drhoo
I've updated my answer with code to help with that.
Tim Fountain
Tim, it worked a treat. Thanks pal, you saved me a big headache. Thanks everyone!
drhoo