Get the Year/Month/Day from a datetime in php?


I used date('w', timestamp) and date('w', timestamp) to know the day, date('n', timestamp) for months, etc.

Now I'm using datetime and I'd like to know what are the equivalent functions to get a day, a month, etc from a datetime.

PS: I know I could use UNIX_TIMESTAMP() in a SQL query but I prefer avoiding timestamps using in my code.

This question is tagged with php

~ Asked on 2011-08-30 17:26:18

The Best Answer is


Use DateTime with DateTime::format()

$datetime = new DateTime($dateTimeString);
echo $datetime->format('w');

~ Answered on 2011-08-30 17:30:12


Check out the manual:

$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');

Will output: 2000-01-01 00:00:00

~ Answered on 2011-08-30 17:32:09

Most Viewed Questions: