strike667 Posted February 4, 2011 Share Posted February 4, 2011 Hello. I think I have a little unique and difficult to solve problem that I really need to post here. I, as a counter-strike portal administrator, am running some CS servers. With automatical script, the server uploads demos (videos of gameplay) on FTP in this format: 101229154428.dem and so on. The number is a date in this format: year, month, day, hours, minutes, seconds => for the example it's 29th December 2010, 15h, 44m and 28s. Then I have a "report" page. The important part on it is that, there is a date in unix timestamp in each row like 1336837680 (29th december 2010, 15h, 47m, 00s). And now what I need: Find a demo that corresponds to the unix timestamp. If there is a timestamp 1336837680, it should find 101229154428.dem. You know what I mean, there is not one demofile, there are plenty. Just find the demo, that contains a record of the time used in timestamp. Pretty difficult, isn't it? I would love to see if anybody came up with anything. Best regards! Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 4, 2011 Share Posted February 4, 2011 It is actually pretty simple to take the unix date and convert it to the format used for the demo files. But, one thing that is not clear in your post above is if there will be an exact match between the unix data and the demo file name. If you are only looking for an exact match this is very simple. If you are looking for a demo file that is closest to the report data you need to provide more information: are you wanting the one that is mathmatically closest or do you need the one that is closest but not past or before??? Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 4, 2011 Share Posted February 4, 2011 By the way, I don't know where you are coming up with those timestamps but 1336837680 is not 29th december 2010, 15h, 47m, 00s it is 12th may 2012, 10h, 48m, 00s (in CST) I don't know what timezone you are in, but that wouldn't acctount for a 2+ year difference! You need to figure out what values you are actually getting if you want to compare them properly. Quote Link to comment Share on other sites More sharing options...
strike667 Posted February 4, 2011 Author Share Posted February 4, 2011 Dunno, put it inside a web converter and this is what came from it. It is actually pretty simple to take the unix date and convert it to the format used for the demo files. But, one thing that is not clear in your post above is if there will be an exact match between the unix data and the demo file name. If you are only looking for an exact match this is very simple. If you are looking for a demo file that is closest to the report data you need to provide more information: are you wanting the one that is mathmatically closest or do you need the one that is closest but not past or before??? Basically yes. Let's say the demo files are created every map change (that's ~20min). Which means, if the demo started recording at 14:00 and another demo at 15:00, if we have a timestamp with 14:35, it should find the demo that started recording at 14:00. Simply the demofile in which the time should be. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 4, 2011 Share Posted February 4, 2011 OK, so you want the first demo file that comes before the report time. The following code will do what you want assuming the date/time used for the file name and for the report timestapm are actually in sync. As I stated above, the timestamp you showed is in the year 2012. Anyway, the following code should work for you. Just be sure to set the correct value for $demoFldr in the getDemoFile() function. Just pass the report date to the function getDemoFile() and it will return the closest, previous demo file for that date from the same day or previous day. If there are none, it will return false. <?php function getDemoFile($rptDate) { $demoFldr = 'demo_files/'; $fileExt = 'dem'; //Convert report date to same format as demofile names for comparison $reportDateTime = date('ymdHis', $rptDate); //Year month day hour min sec //Get demofiles for SAME day of report date $patternDate = date('ymd', $rptDate); $pattern = "{$demoFldr}{$patternDate}??????.{$fileExt}"; $matches = glob($pattern); $demoMatch = prevDemoFile($reportDateTime, $matches, $demoFldr); //Check if match was made for same day if($demoMatch!==false) { return $demoMatch; } //No match made for SAME day, get files for PREV day of report date $patternDate = date('ymd', strtotime(date('d F Y', $rptDate).' -1 day')); $pattern = "{$demoFldr}{$patternDate}??????.{$fileExt}"; $matches = glob($pattern); //Return result (either file from prev day or false) return prevDemoFile($reportDateTime, $matches, $demoFldr); } function prevDemoFile($reportDateTime, $demoFilesAry, $demoFldr) { //Create vars for the output $prevDemoFile = false; $smallestDiff = false; //Find the closest demo file before the report date foreach($demoFilesAry as $demoFile) { $fileDateTime = substr($demoFile, strlen($demoFldr), 12); if( $fileDateTime<$reportDateTime) { if(!$smallestDiff || ($reportDateTime-$fileDateTime)<$smallestDiff) { $smallestDiff = ($reportDateTime-$fileDateTime); $prevDemoFile = $demoFile; } } } return $prevDemoFile; } //Usage $demofile = getDemoFile($report_date); ?> 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.