1

I would like to strtotime dateformat like this - Fri Jun 15 05:38:11 +1000 2012 How could I do that? Basically I need to strtotime it, then do the following - $ago = time()-strtotime(Fri Jun 15 05:38:11 +1000 2012); and then do this -

$ago = $ago / (60*60*24);
echo $ago.' days ago';

So I need one thing - strtotime(Fri Jun 15 05:38:11 +1000 2012) which will work as it need to work.

If this type of date cannot be strtotime, then how can I edit it, so it could be strtotime?

2
  • 3
    See this.
    – jadkik94
    Jun 19, 2012 at 19:19
  • 1
    huh? so you want to input a date like Fri Jun 15 05:38:11 +1000 2012 and have it return '4 days ago'? Jun 19, 2012 at 19:19

3 Answers 3

1

Try this (OOP style, there's a procedural equivalent):

<?php
$str = "Fri Jun 15 05:38:11 +1000 2012";

// From here: http://is2.php.net/manual/en/datetime.createfromformat.php
$datetime = DateTime::createFromFormat('D M d H:i:s T Y', $str);
$now = new DateTime();

// From here: http://is2.php.net/manual/en/datetime.diff.php
$interval = $now->diff($datetime);
echo $interval->format("%R%a days");
?>
0

Compare the difference and use floor/ceil to round.

$now = time();
$then = strtotime($var);

echo floor(($now - $then) / 86400) . " days ago";
1
  • 2
    Yikes! Does strotime("Fri Jun 15 05:38:11 +1000 2012") return what you think it should?
    – Josh
    Jun 19, 2012 at 19:31
0

The DateTime::createFromFormat() method allows you to define the format of the date string that you wish to parse.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.