Page 1 of 1

Need help with Dates in PHP

Posted: Sun Feb 21, 2010 2:45 pm
by ciaranholland
Hi all,

I need some help with using dates in php. My page is getting the date caled 'Due Date' and printing it in a table. See php code:

echo "<tr><td>" . date("M d Y",strtotime($row['DueDate'])) . "</td>";

I want to set variables to the month, date and year. Does anybody know how to do this? I'm new to php and so any help here is much needed and will be much appreciated!

Thank you!

Re: Need help with Dates in PHP

Posted: Sun Feb 21, 2010 4:22 pm
by requinix
The method I use is

Code: Select all

list($year, $month, $day) = explode("-", date("Y-m-d", $whatever));
list, explode

Or you could make three separate calls to date().

Re: Need help with Dates in PHP

Posted: Mon Feb 22, 2010 4:53 am
by wilded1
Using three separate calls to $date ($date is passed as $val in this case), use substr() to get the parts:.
I did this to get day, month and year from a date/time datestamp. You can see what I did here:

Code: Select all

 
// Date from mysql datestamp
function get_date($val){
    $year = substr($val, 0, 4);
    $month = substr($val, 5, 2);
    $day = substr($val, 8, 2);
$date = date('D d M Y', mktime(0, 0, 0, $month, $day, $year));
    return $date;
}
 
// Time from mysql datestamp
function get_time($val){
    $hh=substr($val,11,2);
    $mm=substr($val,14,2);
$time = date('H:i', mktime($hh, $mm));
    return $time;
}