Jump to content

Finding the seconds since last thursday at 5PM


taviguy

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

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