Jump to content

Comparing dates


the_oliver

Recommended Posts

Hello,
I have a scrip which will copy a file only if one is newer then the other.  To do this i work out its age wateing years more then months, months more than days . . . etc.  It was working fine in 2006, but now in 2007 it thinks that the old script is newer.  I know that i need to change the waiting, but my maths is not quite up to this!

So at the moment it is basicly:
[code]$ModDate1 = Year * 10000 + Month * 1000 + Day * 100 + Hour * 10 + Minute[/code]

Can anyone tell me what to do to solve this?

Thanks!
Link to comment
https://forums.phpfreaks.com/topic/34156-comparing-dates/
Share on other sites

You didn't get me. If you enter the input this way:

[table]
[tr]
[td][b]Year || [/b][/td][td][b]Month || [/b][/td][td][b]Day || [/b][/td][td][b]Hour || [/b][/td][td][b]Min || [/b][/td][td][b]Result[/b][/td][/tr]
[tr][td]2006[/td][td]12[/td][td]1[/td][td]0[/td][td]0[/td][td][b]20072100[/b][/td][/tr]
[tr][td]2007[/td][td]2[/td][td]1[/td][td]0[/td][td]0[/td][td][b]20072100[/b][/td][/tr]
[/table]

You see- two different inputs, give the same result when being put inside your formula- you can check if you want!

Orio.
Link to comment
https://forums.phpfreaks.com/topic/34156-comparing-dates/#findComment-160675
Share on other sites

Well, I've been curios about this subject my self, so I wrote this just now

[code=php:0]
<?php
$file1 = mysql_fetch_assoc($q1); //pretend this pulls the stuff for file 1 from the database :p
$file2 = mysql_fetch_assoc($q2); //same as $file1
$strtotime = '%d %s %d %d:%d:00';
$months = array("", "Jan", "Feb", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$file1time = strtotime(sprintf($strtotime, $file1['day'], $months[$file1['month']], $file1['year'], $file1['hour'], $file1['minute']));
$file2time = strtotime(sprintf($strtotime, $file2['day'], $months[$file2['month']], $file2['year'], $file2['hour'], $file2['minute']));

if($file1time == $file2time) {
//do nothing
}
elseif($file1time > $file2time) {
//keep file1
}
elseif($file1time < $file2time) {
//replace file1 with file2
}
else {
//it was unable to convert the string into a unix timestamp...
}
?>
[/code]

What it does is it pulls the values from the database, formats them into a string like "15 Jan 2006 12:56:00"

Then it runs them through PHP's strtotime function which produces a unix time stamp.

It is called a unix time stamp because it is how many minutes since the unix epoch (Jan 1 12:00 AM 1970 if I remember correctly), not because it will only work on unix machines...  Any server with PHP can handles unix timestamps.  Anyways then it compares the timestamps, and since they are straight numbers there's no confusion with weird formulas and what not...
Link to comment
https://forums.phpfreaks.com/topic/34156-comparing-dates/#findComment-160710
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.