PHP checkdate getdate and other date time function operations

  • 2020-03-31 20:29:55
  • OfStack

Checkdate ($month, $date, $year)
This function returns true if the applied value constitutes a valid date. For example, this function returns false for the error date of February 31, 2005.
This function can be used to check the date and make it effective before the date is used for calculation or saved in the database.

 
<?php 
// returns false 
echo checkdate(2,30,2005) ? "valid" : "invalid"; 
// returns true 
echo checkdate(4,6,2010) ? "valid" : "invalid"; 
?> 

Getdate ($ts)
In the absence of an argument, this function returns the current date and time in combination with an array. Each element in the array represents a specific component of the date/time value. An optional time label argument can be submitted to the function to obtain a date/time value corresponding to the time label.
Apply this function to obtain a series of discrete, easily separable date/time values.
 
<?php 
// get date as associative array 
$arr = getdate(); 
echo "Date is " . $arr['mday'] . " " . $arr['weekday'] . " " . $arr['year']; 
echo "Time is " . $arr['hours'] . ":" . $arr['minutes']; 
?> 

Mktime ($hour, $minute, $second, $month, $day, $year)
This function does the opposite of getdate() : it generates a UNIX time label from a series of date and time values (the number of seconds elapsed between January 1, 1970 GMT). When no arguments are used, it generates a UNIX time label for the current time.
Use this function to get the UNIX time label for instant time. Such time tags are commonly used in many database and programming languages.
<?php 
// returns timestamp for 13:15:23 7-Jun-2006 
echo mktime(13,15,23,6,7,2006); 
?> 

The date ($format, $ts)
This function formats the UNIX time label into a human-readable date string. It is the most powerful function in the PHP date/time API and can be used in a series of corrections to convert an integer time label to the desired string format.
This function is applied to display a formatted time or date.
 
<?php 
// format current date 
// returns "13-Sep-2005 01:16 PM" 
echo date("d-M-Y h:i A", mktime()); 
?> 

Strtotime ($STR)
This function converts a human-readable English date/time string into a UNIX time label.
Use this function to convert a non-standardized date/time string into a standard, compatible UNIX time label.
 
<?php 
// returns 13-Sep-05 
echo date("d-M-y", strtotime("today")); 
// returns 14-Sep-05 
echo date("d-M-y", strtotime("tomorrow")); 
// returns 16-Sep-05 
echo date("d-M-y", strtotime("today +3 days")); 
?> 

Strftime ($format, $ts)
As defined in the previous setlocale() function, this function formats the UNIX time label into a date string that applies to the current environment.
Use this function to create a date string that is compatible with the current environment.
 
<?php 
// set locale to France (on Windows) 
setlocale(LC_TIME, "fra_fra"); 
// format month/day names 
// as per locale setting 
// returns "septembre" and "mardi" 
echo strftime("Month: %B "); 
echo strftime("Day: %A "); 
?> 

Microtime ()
As defined in the previous setlocale() function, this function formats the UNIX time label into a date string that applies to the current environment.
Use this function to create a date string that is compatible with the current environment.
 
<?php 
// get starting value 
$start = microtime(); 
// run some code 
for ($x=0; $x<1000; $x++) { 
$null = $x * $x; 
} 
// get ending value 
$end = microtime(); 
// calculate time taken for code execution 
echo "Elapsed time: " . ($end - $start) ." sec"; 
?> 

Gmmktime ($hour, $minute, $second, $month, $day, $year)
This function generates a UNIX time label from a series of date and time values represented by GMT time. When no arguments are used, it generates a UNIX time label for the current GMT instant time.
Use this function to get the UNIX time label for the GMT instant time.
 
<?php 
// returns timestamp for 12:25:23 9-Jul-2006 
echo gmmktime(12,25,23,7,9,2006); 
?> 

The gmdate ($format, $ts)
This function formats the UNIX time label into a human-readable date string. This date string is represented as GMT (not local time).
Apply this function when the time label is denoted by GMT.
 
<?php 
// format current date into GMT 
// returns "13-Sep-2005 08:32 AM" 
echo gmdate("d-M-Y h:i A", mktime()); 
?> 

Date_default_timezone_set ($tz), date_default_timezone_get ()
All subsequent date/time function calls are set and the default time zone is restored.
Note: this function is only valid in PHP 5.1+.
This function is a convenient shortcut to set the time zone for future time operations.
 
<?php 
// set timezone to UTC 
date_default_timezone_set('UTC'); 
?> 


Related articles: