php DateTime converting unix timestamp
A question that I have is why doesn't the DateTime class convert the unix epoc by default Example would be
function convert($date){
$d = new DateTime($date);
echo $d;
}
$now = time();
convert($now);
I mean sure there is 20 different date/time functions in php but one of the most common formats Errors can somebody shed some light on if i am missing something without passing a string time through 2 other functions "messy code" to p开发者_JAVA技巧ass it to the DateTime Class ? function convert($date){
$d = new DateTime($date);
echo $d;
}
$now = time();
convert('@'.$now);
example #2 http://www.php.net/manual/en/datetime.construct.php
This converts seconds since the Unix Epoch to DateTime:
$my_php_datetime_object = DateTime::createFromFormat('U', $seconds_epoch)
http://php.net/manual/en/datetime.createfromformat.php
Try looking at this site... It has helped me many times in different languages. http://www.epochconverter.com/
so i think you want this:
function convert($date)
{
$d = date("r", $date);
echo $d;
}
$now = time();
convert($now);
echo "<br><br>";
convert("1296928800");
echo "<br><br>";
# 60 * 60 * 24 * 365 * 40
# secs mins hours days years
convert("1296928800" + (60 * 60 * 24 * 365 * 40));
echo "<br><br>";
convert($now + (60 * 60 * 24 * 365 * 40));
精彩评论