I needed this, and didn’t find a ready-made solution (there are, no doubt, billions of ready-made solutions, but I refuse to go searching through those hideous directories of code snippets to find them). It seems to work with all modern dates, and I’ve tested it reasonably well, trying a number of different cases, but as always YMMV.
The arguments are UNIX-style timestamps (seconds since the 1/1/70).
function time_diff($from, $to) {
if ($from > $to) {
$t = $to;
$to = $from;
$from = $t;
}
$year1 = date("Y", $from);
$year2 = date("Y", $to);
$month1 = date("n", $from);
$month2 = date("n", $to);
$day1 = date("j", $from);
$day2 = date("j", $to);
if ($day2
Examples:
n”;
$t1 = mktime(0,0,0,4,29,1980);
$t2 = time();
$t3 = mktime(0,0,0,8,26,2003);
echo “
I’ve been alive: ” . time_diff($t1, $t2) . “
“;
echo “Time until I go to China: ” . time_diff($t2, $t3) . “
“;
?>
What does it do? I can tell it has something to do with time, and that it spits out a time stamp in YY,MM,DD format, but I can’t tell why you needed it, what it does, or where in the code you will put it.
Also, how do you get color to show up in movable type when authoring?
Left by David M on July 15th, 2003
Ahh, yeah, I guess I did leave all of that out, didn’t I?
OK, let’s see: what it does is return the difference between two datestamps in years, months and days (I’ll add an example in just a minute to show you the output). I needed it because there is no built-in PHP functions that can do it… you can tell the number of days, minutes or seconds between two timestamps, but not larger units. I needed to be able to do that for a project I’ve been working on, and it can be included as a function anywhere in your code, should you choose to use it, though for me it goes in a file full of functions that gets included in the script.
The movabletype coloring is accomplished using Brad Choate’s MTTextile plugin and a few other third party utilities. It’s neat, isn’t it?
Left by John on July 15th, 2003
Yeah, it’s really neat.
Jordan set up MovableType for me on his server and I want to use it, but I also want it to look a little different and perform a few more functions. So, I thought I would start by stealing the HTML from this site, which worked until I read a little of it and realized that you didn’t write it. That’s when I freaked out. Luckily, I noticed that there was some .php thing at the end of the URL, and decided to start with Googling that. Now, I am trying to learn .php at php.net and phpcomplete.com, but it is a slow process. It also doesn’t help that I am really afraid to mess up the ‘main’ template too much, because I don’t know how to get it back or how to save it somewhere on my computer without just copying all the code and saving that in a notebook file.
So, basically the point of this e-mail is that you inspired me, and I hope to learn a lot of this stuff in the months to come.
Left by David M on July 15th, 2003
It’s flattering to know that I’ve inspired something. To set the record straight, though, I coded _everything_ myself except for the Javascript to pop open a comment window (it’s the default stuff that came with MovableType, and I decide not to reinvent the wheel). Everything else is mine.
Left by John on July 15th, 2003
When he said you didn’t write it he meant he could tell the html was generated, I think, not that you didn’t write the code that generated it.
Left by Jordan on July 15th, 2003
.Not is this English language or easy clear always
Left by David M on July 15th, 2003
John, in MT, what are the “allow ping” and “send ping to URLs” features all about? I’ve seen them, read the help files on them, but I still don’t quite get it.
Like, if I were to type in a URL what would happen next? Is it something I can undo?
Left by David M on July 27th, 2003
Just for testing
Left by kumar on April 19th, 2004
This doesn’t work for the dates 2004-04-04 and 2004-04-05. It thinks there’s no difference in days, months or years.
Left by Charles Koehl on June 4th, 2004
I just tested it and it seems to work fine: echo time_diff(strtotime(’2004-04-04′), strtotime(’2004-04-05′)); returns “0 years, 0 months, 1 days” like it’s supposed to. Make sure the arguments you’re feeding it are Unix-style timestamps, or else it won’t work right.
Left by John on June 4th, 2004
The script works well, however, I do have one question. if my to date is the current date eg 2004-8-24 and my from date is in the past 2004-8-24 then the output will be : 0 years, 1 months, 0 days. If my from date is in the future 2004-9-24 then the output will be : 0 years, 1 months, o days. How can I display a negative value if the from date is past the current date? eg
0 years, -1 months, 0 days?
Left by Mark on August 24th, 2004
Thank you for the script John. I’m new to PHP, and it saves me at least an hour of work.
Left by Dmitry on March 5th, 2005
Just added some code at the bottom. Certainly this is a great function, but there is no need to show “0 years , 0 months , 0 days” … if there is not a big difference between the given dates, may be we should give to the user a more friendly message:
this is how i did:
$ret = “”; // initialize var
if($years != 0) $ret .= “$years years”; // if is an year
if($months != 0 && $years != 0) $ret .= ” , “; // add separator
if($months != 0) $ret .= ” $months months”; // if is an month
if($days != 0 && $months != 0) $ret .= ” , “; // add separator
if($days != 0) $ret .= ” $days days”; // if is an day
if($ret == “” && $days == 0) $ret.= “today”; // if is today
return $ret;
Left by Carlos Garcia on April 7th, 2005
or something like thath
$ret = “”; // initialize
if($years != 0) $ret .= “$years aƱos “; // if is an year
if($months != 0 && $years != 0) $ret .= “,”; // add separator
if($months != 0) $ret .= ” $months meses “; // if is an month
if($days != 0 && $months != 0) $ret .= “,”; // add separator
if($days != 0) $ret .= ” $days dias “; // if is an day
if($ret == “” && $days == 0) $ret.= “hoy mismo”; // if is today
return “$ret”;
Left by Carlos Garcia on April 7th, 2005
well… there is a bug in this forum when you do copy - paste code …!
the code i posted didn’t posted full… i’ll try again
$ret = “”;
if($years != 0) $ret .= “$years years”; // if is an year
if($months != 0 && $years != 0) $ret .= ” , “; // add a separator if needed
if($months != 0) $ret .= ” $months months”; // if is an month
if($days != 0 && $months != 0) $ret .= ” , “; // add separator if needed
if($days != 0) $ret .= ” $days days “; // if is an day
if($ret == “” && $days == 0) $ret.= “today”; // if is today
return $ret;
if this didn’t posted full… write and i’ll give you by mail.
Left by Carlos Garcia on April 7th, 2005
suggested next forum thread:
how to put code in a FORUM! …
Left by Carlos Garcia on April 7th, 2005
Vietnam tours designer, hotels reservation, resorts reservation, ticket, property & car rental…
Left by Hai Bui on June 28th, 2005