PHP - How to get year, month, day from time string
You can use strtotime
to parse a time string, and pass the resulting timestamp to getdate
(or use date
to format your time).
$str = '2000-11-29';
if (($timestamp = strtotime($str)) !== false)
{
$php_date = getdate($timestamp);
// or if you want to output a date in year/month/day format:
$date = date("Y/m/d", $timestamp); // see the date manual page for format options
}
else
{
echo 'invalid timestamp!';
}
Note that strtotime
will return false
if the time string is invalid or can't be parsed. When the timestamp you're trying to parse is invalid, you end up with the 1969-12-31 date you encountered before.
PHP - How to get year, month, day from time string
$dateValue = strtotime($q);
$yr = date("Y", $dateValue) ." ";
$mon = date("m", $dateValue)." ";
$date = date("d", $dateValue);
Update: Forgot to add semicolon at end of first line, try this:
<?php
$str = "2010-08-29"; // Missed semicolon here
$time = strtotime($str);
// You can now use date() functions with $time, like
$weekday = date("l", $time); // Wednesday or whatever date it is
?>
Hopefully that will get you going!