The PHP date() function formats a timestamp to a more readable date and time
A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred
Some characters that are commonly used for date and time:
d - Represents the day of the month (01 to 31)
m - Represents a month (01 to 12)
Y - Represents a year (in four digits)
l (lowercase 'L') - Represents the day of the week
h - 12-hour format of an hour with leading zeros (01 to 12)
i - Minutes with leading zeros (00 to 59)
s - Seconds with leading zeros (00 to 59)
a - Lowercase Ante meridiem and Post meridiem (am or pm)
The following code illustrates date function in PHP:
// display the date in the format YYYY/MM/DD
echo "Today is " . date("Y/m/d") . "<br>";
// ‘l’ is used to display the day echo "Today is " . date("l"). "<br>";
// display the time in the format HH:MM:SS
echo "The time is " . date("h:i:sa");
?> </body> </html>
SYNTAX : DATE FUNCTION
date('H:i, jS F')
Notice that it passes a string (text data) to the function inside a pair of parentheses. The element within the parentheses is called the function’s argument or parameter. Such arguments are the input the function uses to output some specific results.
The date() function expects the argument you pass it to be a format string, representing the style of output we would like. Each letter in the string represents one part of the date and time. H is the hour in a 24-hour format with leading zeros where required, i is the minutes with a leading zero where required, j is the day of the month without a leading zero, S represents the ordinal suffix (in this case th), and F is the full name of the month.
VALIDATING DATES WITH CHECKDATE()
You can use the checkdate() function to check whether a date is valid. This capability is especially useful for checking dates constructed from user input. The checkdate() function has the following prototype:
int checkdate(into month,into day,into year)
Validating Dates with checkdate()
we can format a timestamp according to the system’s locale (the web server’s local settings) using the strftime() function. This function has the following prototype:
string strftime(string $format [, int $timestamp])
The $format parameter is the formatting code that defines how the timestamp will be displayed. The $timestamp parameter is the timestamp that we pass to the function. This parameter is optional.
The following code illustrates to display timestamp in four different format
php echo strftime('%A<br />');
echo strftime('%x<br />');
echo strftime('%c<br />');
echo strftime('%Y<br />');
The above code will produce 4 different timestamp
Sat Jan 09 12:55:19 2021
CALCULATING DATES IN PHP
A simple way to work out the length of time between two dates in PHP is to use the difference between Unix timestamps. The following script calculate the difference between two dates
// set date for calculation
$day = 18;
$month = 9;
$year = 1972;
// remember you need bday as day month and year
$bdayunix = mktime (0, 0, 0, $month, $day, $year); // get ts for then
$nowunix = time(); // get unix ts for today
$ageunix = $nowunix - $bdayunix; // work out the difference
$age = floor($ageunix / (365 * 24 * 60 * 60)); // convert from seconds to years
echo 'Current age is '.$age.'.';
This script sets the date for calculating the age. In a real application, it is likely that this information might come from an HTML form.
For some applications, measuring time in seconds is not precise enough to be useful. If we want to measure very short periods, such as the time taken to run some or all of a PHP script, we need to use the function
echo number_format(microtime(true), 5, '.', '');
The above line of code
will produce output like 1424141373.59059.
USING THE CALENDAR FUNCTIONS
PHP has a set of functions that enable you to convert between different calendar systems. The main calendars you will work with are the Gregorian, Julian, and Julian Day Count.
Most Western countries currently use the Gregorian calendar. The Gregorian date October 15, 1582, is equivalent to October 5, 1582, in the Julian calendar. Prior to that date, the Julian calendar was commonly used. Different countries converted to the Gregorian calendar at different times and some not until early in the twentieth century.
The functions we would use to convert from the Gregorian calendar to the Julian calendar:
int gregoriantojd (int month, int day, int year)
string jdtojulian(int julianday)
To convert a date, you would need to call both of these functions:
$jd = gregoriantojd (9, 18, 1582);
This call echoes the Julian date in a MM/DD/YYYY format.