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 ?

1
  • 1
    or would i call use DateTime->format to read the unix timestamp ? Commented Feb 5, 2011 at 4:05

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

also you can just do convert("now");
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

Comments

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));

2 Comments

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]stackoverflow.com/questions/1766208/…
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.