Jump to content

[SOLVED] Countdown to first tuesday of every month.


TRemmie

Recommended Posts

this would be it roughly and I think you might have trouble if you're dealing with a non standard timestamps or timezone outside of the US time zones. 

 

 
$thistues=strtotime("first tuesday");
$today=strtotime(date("Y-m-d"));
$nexttues=($today<$thistues)?$thistues:date("Y-m-d", strtotime("first tuesday", strtotime("+ 1 Month", strtotime(date("Y-m")))));;
$seconds=($nexttues-$today);
$minutes=($seconds/60);
$hours=($minutes/60);
$days=($hours/24);
echo "There are ".$days." days left until next tuesday";

This is echoing back "There are 7 days left until next tuesday" but we are looking for "24 days remaining till the next first tuesday".  :shrug::confused:

 

$thistues=strtotime("first tuesday");
$today=strtotime(date("Y-m-d"));
$nexttues=($today<$thistues)?$thistues:date("Y-m-d", strtotime("first tuesday", strtotime("+ 1 Month", strtotime(date("Y-m")))));;
$seconds=($nexttues-$today);
$minutes=($seconds/60);
$hours=($minutes/60);
$days=($hours/24);
echo "There are ".$days." days left until next tuesday";

try this instead

$thistues=strtotime("first tuesday",strtotime(date("Y-m")));
$today=strtotime(date("Y-m-d"));
$nexttues=($today>=$thistues)?strtotime("first tuesday", strtotime("+ 1 Month", strtotime(date("Y-m")))):$thistues;
$seconds=($nexttues-$today);
$minutes=($seconds/60);
$hours=($minutes/60);
$days=($hours/24);
echo "There are ".floor($days)." days left until next tuesday";

 

If you're up for using some of the really nice additions in PHP 5.3, then you could also use the following:

 

<?php

$now  = new DateTime();
$tues = new DateTime('first tuesday of next month');
$diff = $now->diff($tues);
echo $diff->days;

?>

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.