the_oliver Posted January 14, 2007 Share Posted January 14, 2007 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! Quote Link to comment Share on other sites More sharing options...
Orio Posted January 14, 2007 Share Posted January 14, 2007 You should use timestamps (unix time). Then you could do it easily using strtostime(). Google and read about unix time in php.net. It will make your life easier.Orio. Quote Link to comment Share on other sites More sharing options...
the_oliver Posted January 14, 2007 Author Share Posted January 14, 2007 Thanks, it would make life simpler, but i need to do it this way really. Its something that i dont just use in .php scrips so would like to know what im doing wrong for others also. + the scrip is running on windows files. Quote Link to comment Share on other sites More sharing options...
Orio Posted January 14, 2007 Share Posted January 14, 2007 But you'll never get it right this way... For an example:2006 12 1 0 0 = 200721002007 2 1 0 0 = 20072100Two different inputs- same result...Orio. Quote Link to comment Share on other sites More sharing options...
the_oliver Posted January 14, 2007 Author Share Posted January 14, 2007 Thats why i times the diffrent parts by diffrent size numbers, so a to waite them. Quote Link to comment Share on other sites More sharing options...
Orio Posted January 14, 2007 Share Posted January 14, 2007 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. Quote Link to comment Share on other sites More sharing options...
corbin Posted January 14, 2007 Share Posted January 14, 2007 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.