taviguy Posted April 12, 2007 Share Posted April 12, 2007 I have been trying to come up with the logic to calculate the # of seconds since the last occuring thursday at 5PM. I have not worked with PHP or really any programming in a while, and I am not fully familiar with all of the different date functions. I did some research and I came up with some really really messy and probably incorrect logic to try to come up with this. $days_since_last_thursday = if it is fri, sat, sun (date('w') > 4), date('w') - 4 if it is mon, tue, wed (date('w')<4), date('w') + 4 if it is thursday and earlier than 5PM 6 days $seconds_thus_far_today = if time < 5pm (date('G') < 17) (date('G')*60*60) + // seconds in the hours thus far today (date('i')*60) + // seconds in the minutes thus far this hour date('s') // seconds of current minute $seconds_since_last_thurs = (60*60*24*$days_since_last_thursday) + $seconds_thus_far_today $today = time(); $last_thursday = $today - $seconds_since_last_thursday No, I never thought that would actually work and yes I know that isnt even anything close to php syntax - I am just trying to come up with the logic. Could you all help me come up with the best way to return the number of seconds since last thursday? Feel free to completely disregard the pseudocode snippet i have worked on already, its probably completely wrong. Thanks! - Rick Tanner Link to comment https://forums.phpfreaks.com/topic/46741-finding-the-seconds-since-last-thursday-at-5pm/ Share on other sites More sharing options...
veridicus Posted April 12, 2007 Share Posted April 12, 2007 Use strtotime to get the unix timestamp for "last thursday" and subtract it from the current timestamp. I know it looks funny to type strtotime("last Thursday"), but it works and it's really handy. Link to comment https://forums.phpfreaks.com/topic/46741-finding-the-seconds-since-last-thursday-at-5pm/#findComment-227785 Share on other sites More sharing options...
taviguy Posted April 12, 2007 Author Share Posted April 12, 2007 wow that is really simple. i would have used 20 lines of code to do something so simple. will that return 00:00:00 on that day, or the current time? Thanks! Link to comment https://forums.phpfreaks.com/topic/46741-finding-the-seconds-since-last-thursday-at-5pm/#findComment-227812 Share on other sites More sharing options...
veridicus Posted April 12, 2007 Share Posted April 12, 2007 Oh, good question. I forgot about the 5pm. You can say "last Thursday 5pm". The only issue is time zone. But here's a little test I wrote... <?php $time = strtotime('last Thursday'); echo date('r', $time); echo '<br/><br/>'; $time = strtotime('last Thursday 5pm'); echo date('r', $time); ?> And i get this output: Thu, 05 Apr 2007 00:00:00 -0400 Thu, 05 Apr 2007 17:00:00 -0400 So just remember to pay attention to time zone. Link to comment https://forums.phpfreaks.com/topic/46741-finding-the-seconds-since-last-thursday-at-5pm/#findComment-227825 Share on other sites More sharing options...
taviguy Posted April 12, 2007 Author Share Posted April 12, 2007 Thanks! I would have just tested this stuff on my own, but I do not have access to a php capable server right now, I am just doing this for a friend. Thanks again! Link to comment https://forums.phpfreaks.com/topic/46741-finding-the-seconds-since-last-thursday-at-5pm/#findComment-227828 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.