jeudi 28 juillet 2016

Doing math with Timestamps?

To the best of my limited knowledge and abilities with PHP the following code should work:

<?php
////DISPLAY DATE OF NEXT COUNCIL MEETING////

$now = date('U'); //get current time
$firstTues = strtotime("-1 month first Tuesday 4pm"); //get first Tuesday of the month
$secondTues = strtotime("-1 month second Tuesday 5pm"); //get second Tuesday of the month
$fourthTues = strtotime("-1 month fourth Tuesday 5pm"); //get forth Tuesday of the month
$nextTues = strtotime("first Tuesday 4pm"); //get first Tuesday of next month

function nextCouncilMeeting() {

//If todays date less than 1st Tuesday at 11pm, display date for 1st Tuesday 4pm.
if ($now < $firstTues) {
    echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A', $firstTues);
}

//If todays date greater than 1st Tuesday 5pm and less than 2nd Tuesday 11pm, display date for 2nd Tuesday 5pm
elseif ($now > $firstTues and $now < $secondTues) {
    echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A', $secondTues);
}

//If todays date greater than 2nd Tuesday 5pm and less that 4th Tuesday 11pm, display date for 4th Tuesday 5pm
elseif ($now > $secondTues and $now < $fourthTues) {
    echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A', $fourthTues);
} 

//If todays date greater than 4th Tuesday
elseif ($now > $fourthTues){
    echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A', $nextTues);
}
else{
    echo "foobar";
}
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p id="test">
Current Time: <?php echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A',$now); echo " " . $now;?></br>
First Tuesday: <?php echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A',$firstTues);echo " " . $firstTues;?></br>
Second Tuesday: <?php echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A',$secondTues);echo " " . $secondTues;?></br>
Fourth Tuesday: <?php echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A',$fourthTues);echo " " . $fourthTues;?></br>
Next Month First Tuesday: <?php echo date('F j\<\s\u\p\>S\</\s\u\p\> \a\t g:i A',$nextTues);echo " " . $nextTues;?>
</p>
<h2>Next Council Meeting:</h2>
<h1><?php nextCouncilMeeting()?></h1>
</body>
</html>

My best guess is that there's something going wrong with my math in the if/elsif conditions. Is there a trick to doing math with timestamps? What am I missing here?

Aucun commentaire:

Enregistrer un commentaire