bschultz Posted July 19, 2011 Share Posted July 19, 2011 I need to check a file's timestamp (last modified) and if it's BEFORE 15 minutes past the current hour (xx:15:00) then run a shell command. Here's the code <?php ///////// start of time constants ///////// date_default_timezone_set('US/Central'); $fullyear = date('Y'); $year = date('y'); $month = date('m'); $day = date('d'); $now = date('Y-m-d-h'); $hour = date('g'); $hourplus1 = date('g', strtotime("Now +1 hour")); $hourminus1 = date('g', strtotime("Now -1 hour")); $ampm = date('A'); $rename_date = date('mdg'); $nowis = date('m'); $copy1 = "/home/rpbroadcasting/audio/S$rename_date.mp3"; if (file_exists($copy1)) { echo system('normalize-mp3 --mp3 '.escapeshellcmd($copy1)); $stamp1 = date ("n-j-Y g:i:s A.", filemtime($copy1)); $fifteenpastthishour = mktime(date("n"), date("j"), date("y"), date("g"), 15); if ($stamp1 <= $fifteenpastthishour) { echo system(escapeshellcmd("mp3gain -g 3 $copy1")); } } $copy2 = "/home/rpbroadcasting/audio/N$rename_date.mp3"; if (file_exists($copy2)) { echo system('normalize-mp3 --mp3 '.escapeshellcmd($copy2)); $stamp2 = date ("n-j-Y g:i:s A.", filemtime($copy2)); $fifteenpastthishour = mktime(date("n"), date("j"), date("y"), date("g"), 15); if ($stamp2 <= $fifteenpastthishour) { echo system(escapeshellcmd("mp3gain -g 1 $copy2")); } } $copy3 = "/home/rpbroadcasting/audio/W$rename_date.mp3"; if (file_exists($copy3)) { echo system('normalize-mp3 --mp3 '.escapeshellcmd($copy3)); $stamp3 = date ("n-j-Y g:i:s A.", filemtime($copy3)); $fifteenpastthishour = mktime(date("n"), date("j"), date("y"), date("g"), 15); if ($stamp3 <= $fifteenpastthishour) { echo system(escapeshellcmd("mp3gain -g 1 $copy3")); } } ?> The problem is, it's ALWAYS running the mp3gamin shell command...no matter what the timestamp of the file is. Where am I going wrong with this? Thanks. Brian Link to comment https://forums.phpfreaks.com/topic/242352-checking-file-modified-timestamp/ Share on other sites More sharing options...
djlee Posted July 19, 2011 Share Posted July 19, 2011 stamp1 is a date string, fifteenpastthishour is an INT Link to comment https://forums.phpfreaks.com/topic/242352-checking-file-modified-timestamp/#findComment-1244736 Share on other sites More sharing options...
requinix Posted July 19, 2011 Share Posted July 19, 2011 Also, while it's possible the compare date strings, they have to be formatted according to some rules (which your "n-j-Y g:i:s A." does not follow). It's easier to compare numbers. Link to comment https://forums.phpfreaks.com/topic/242352-checking-file-modified-timestamp/#findComment-1244764 Share on other sites More sharing options...
bschultz Posted July 19, 2011 Author Share Posted July 19, 2011 Thanks for the help! After re-thinking this, I need to make sure that the mp3gain command only runs once per hour. I think I'll have to store the count in a flat file...and compare the count. Then, I won't have to compare the timestamp... Thanks again, though! Link to comment https://forums.phpfreaks.com/topic/242352-checking-file-modified-timestamp/#findComment-1244790 Share on other sites More sharing options...
requinix Posted July 19, 2011 Share Posted July 19, 2011 Once an hour? The best way to do that is to make a cron job (Linux) or scheduled task (Windows). Then the script actually only runs once an hour - no messing with timestamps or dates. Link to comment https://forums.phpfreaks.com/topic/242352-checking-file-modified-timestamp/#findComment-1244838 Share on other sites More sharing options...
bschultz Posted July 19, 2011 Author Share Posted July 19, 2011 The script to download these mp3's runs at xx:05:xx of each hour (via cron) ...and then checks if the file exists every five minutes after that (xx:10:00, xx:15:xx, xx:20:xx...etc) to accommodate a file not being present on the remote server at xx:05:xx of the hour. The normalize script (also runs via cron) also needs to run every five minutes...to accommodate a file not being present on the remote server at xx:05:xx of the hour....but I only want to normalize the mp3 file once in the hour...not every five minutes that the file exists...so was going to check the timestamp. Then, I realized that method wouldn't work either...if the file downloaded after 15 minutes past the hour...it would normalize every five minutes. So...that's why I went with a flat file and a counter on that file. <?php ///////// start of time constants ///////// date_default_timezone_set('US/Central'); $fullyear = date('Y'); $year = date('y'); $month = date('m'); $day = date('d'); $now = date('Y-m-d-h'); $hour = date('g'); $hourplus1 = date('g', strtotime("Now +1 hour")); $hourminus1 = date('g', strtotime("Now -1 hour")); $ampm = date('A'); $rename_date = date('mdg'); $nowis = date('m'); $copy1 = "/home/rpbroadcasting/audio/S$rename_date.mp3"; if (file_exists($copy1)) { $fp = fopen("/var/www/sportscount.txt", "r"); $count = fread($fp, 1024); fclose($fp); $count = $count + 1; if ($count == 1) { echo system('normalize-mp3 --mp3 '.escapeshellcmd($copy1)); echo system(escapeshellcmd("mp3gain -g 3 $copy1")); //$count = 0; echo "Count is 1...I have to normalize\n"; } $fp = fopen("/var/www/sportscount.txt", "w"); fwrite($fp, $count); fclose($fp); } $copy2 = "/home/rpbroadcasting/audio/N$rename_date.mp3"; if (file_exists($copy2)) { $fp = fopen("/var/www/newscount.txt", "r"); $count = fread($fp, 1024); fclose($fp); $count = $count + 1; if ($count == 1) { echo system('normalize-mp3 --mp3 '.escapeshellcmd($copy2)); echo system(escapeshellcmd("mp3gain -g 1 $copy2")); //$count = 0; echo "Count is 1...I have to normalize\n"; } $fp = fopen("/var/www/newscount.txt", "w"); fwrite($fp, $count); fclose($fp); } $copy3 = "/home/rpbroadcasting/audio/W$rename_date.mp3"; if (file_exists($copy3)) { $fp = fopen("/var/www/weathercount.txt", "r"); $count = fread($fp, 1024); fclose($fp); $count = $count + 1; if ($count == 1) { echo system('normalize-mp3 --mp3 '.escapeshellcmd($copy3)); echo system(escapeshellcmd("mp3gain -g 1 $copy3")); //$count = 0; echo "Count is 1...I have to normalize\n"; } $fp = fopen("/var/www/weathercount.txt", "w"); fwrite($fp, $count); fclose($fp); } ?> The above is working as I want it to...then I run another (via cron) script at the the top of the hour to reset the counts to 0. Thanks again everyone! Link to comment https://forums.phpfreaks.com/topic/242352-checking-file-modified-timestamp/#findComment-1244880 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.