Need help with Dates in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ciaranholland
Forum Newbie
Posts: 6
Joined: Sat Feb 20, 2010 6:50 pm

Need help with Dates in PHP

Post 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!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need help with Dates in PHP

Post 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().
wilded1
Forum Newbie
Posts: 9
Joined: Sun Feb 21, 2010 11:49 am

Re: Need help with Dates in PHP

Post 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;
}
 
 
Post Reply