+

PHP date and time

Date and time are some of the most commonly used operations in PHP when making SQL queries or developing a website, etc. PHP provides us with predefined functions for these tasks. Some of the predefined PHP date and time functions are discussed below. PHP date() Function PHP date() function converts a timestamp into a more readable date and time format.Why do we need the date() function?
The computer stores dates and times in UNIX Timestamp format, which measures time as a number of seconds since the beginning of the Unix era (GMT at midnight January 1, 1970, i.e., January 1, 1970, 00:00:00 GMT). 
Since this format is impractical for human readability, PHP converts the timestamp into a format that is human readable and understandable. 
Syntax:
date (format, timestamp) 
Explanation :
  • The format parameter to the date() function specifies the format of the date and time returned.
  • The timestamp is optional, if not enabled, the current date and time will be used.
  • Below the program explains the use of the date() function in PHP: echo "Today`s date is:" ; $today = date ( "d / m / Y" ); echo $today ;  
    ?>
    Output:
    Today`s date is: 05/12/2017 
    Formatting options available in the date() function : The format parameter of the date() function is a string that can contain multiple characters to generate dates in different formats. 
    Date formatting characters that are commonly used in a format string:
    • d - represents the day of the month; two digits with leading zeros (01 or 31).
    • D - represents the day of the week in text as an abbreviation (Mon-Sun).
    • m - represents the month in numbers with leading zeros (01 or 12).
    • M - represents the month in text, abbreviated (January to December).
    • y - represents the year in two digits (08 or 14).
    • Y - represents the year in four digits (2008 or 2014).
    • Parts of the date can be separated by inserting other characters such as hyphens (-), periods (.), forward slashes (/), or spaces to add additional visual formatting.
    echo "Today`s date in various formats:" . "" ; echo date ( " d / m / Y " ). "" ; echo date ( "dmY" ). "" ; echo date ( "dmY" ). "" ; echo date ( "dMY / D" ); ?> Output:
    Today`s date in various formats: 05/12/2017 05-12-2017 05.12.2017 05.Dec.2017 / Tue 
    The following characters can be used in conjunction with the date() function to time string formatting:
    • h - represents the hour in 12-hour format with leading zeros (01 through 12).
    • H - represents hours in 24-hour format with leading zeros (00 to 23).
    • i - represents minutes with leading zeros (00 through 59).
    • s - represents seconds with leading zeros (00 through 59).
    • a - represents lower case before the meridian and after the meridian (morning or evening).
    • A - represents uppercase before the meridian and after the meridian (AM or PM).
    echo date ( "h : i: s " ). "" ; echo date ( "M, d, Y h: i: s A" ). "" ; echo date ( "h: ia" ); ?> Output:
    03:04:17 Dec, 05,2017 03:04:17 PM 03:04 pm 
    PHP time() Function The time() function is used to get the current time as a Unix timestamp (seconds since the beginning of the Unix epoch: January 1, 1970 00:00:00 GMT). The following characters can be used to format the time string:
    • h - represents the hour in 12-hour format with leading zeros (01 through 12).
    • H - represents hours in 24-hour format with leading zeros (00 to 23).
    • i - represents minutes with leading zeros (00 through 59).
    • s - represents seconds with leading zeros (00 through 59).
    • a - represents lower case before the meridian and after the meridian (morning or evening).
    • A - represents uppercase before the meridian and after the meridian (AM or PM).
    The program below explains the use of the time() function in PHP: $timestamp = time(); echo ( $timestamp ); echo "" ; echo ( date ( "F d, Y h: i: s A" , $timestamp ));  
    ?>
    Output:
    1512486297 December 05, 2017 03:04:57 PM 
    PHP mktime() function mktime() function is used to creating a timestamp for a specific date and time. 
    If no date and time are specified, the timestamp is returned for the current date and time.Syntax:
    mktime (hour, minute, second , month, day, year) 
    Inputdata : echo mktime (23 , 21, 50, 11, 25, 2017); ?> Output:
    1511652110 
    The above code creates a timestamp for November 25, 2017, 23 hours 21 minutes 50 seconds.
    Get Solution for free from DataCamp guru