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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.