PHP Date() Basics – Countdown to the Weekend

I have used Date() to feed variables from some complex time tracking scheduling scripts but for the most part it’s use just display time & date, as well as generate the time of an event to log for reference. To get a basic understanding of it, I wrote a simple script using two native functions of Date() N & l (lowercase L). N – Gives you the number of the week, starting from Monday (1), then Tuesday (2), . . . ending with Sunday (7). l – Return the day of the week in a more visual pleasing way; no order just returns the current value which will be the full name of the day of the week. Below, to show how it works I assembled three variables and an if statement to display a count down to the weekend or Say enjoy the weekend.

<?php
//Countdown to the weekend

$numOfTheWeek = date('N');
// l (lowercase L) full textual representation of the day of the week
// Output: Sunday through Saturday

$dayOfTheWeek = date('l');
// N is a ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)
// Output: 1 (for Monday) through 7 (for Sunday)

if($numOfTheWeek <= 5 ) { // 5 is Friday
// Alternate : if($numOfTheWeek < 6 ) { // before Saturday
	$countdown = 6 - $numOfTheWeek;
	// Since the weekend starts Saturday not Sunday subtract the current number of the week from 6
	echo "It's $dayOfTheWeek Meaning you have $countdown days until the weekend!";
}else{
	echo "It's $dayOfTheWeek! Enjoy your weekend!";
};
?>






Leave a Reply



blog