2

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 pass it to the DateTime Class ?
dominicdinada
  • 41
  • 1
  • 2

3 Answers3

5
function convert($date){
$d = new DateTime($date);
echo $d;
}
$now = time();
convert('@'.$now);

example #2 http://www.php.net/manual/en/datetime.construct.php

CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
1

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

dev.e.loper
  • 35,446
  • 76
  • 161
  • 247
0

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));
hitmanDX
  • 245
  • 1
  • 5
  • I do thank you for the replies I however just used the time() as now for the epoc what if i am pulling the time from the database in epoc the time would still be a string in epoc to what ever time it values to, while this is a .net example of what would be the functions i am looking for it still is a little messy constructing the class with strtotime format then adding 40 years in epoc seconds here is the link [link]http://stackoverflow.com/questions/1766208/unix-timestamp-to-net-datetime[/link] – dominicdinada Feb 05 '11 at 15:31
  • I updated my example for you, the value can be passed as a string/int/long since PHP really doesn't care most of the time what the var type is. Also displayed how you would add 40 years to it. Let me know if that helps. – hitmanDX Feb 05 '11 at 18:13