PHP - Date and Time

    PHP allows you to use many date functions along with many modifiers that help you to perform some operations on a date. You can even manipulate date using PHP date and time functions. Use of time() function If you want all the information about the current date and time, then PHP helps you with time() function. This function does not take any argument and returns an integer value regarding the date and time. This returned integer value denotes the number of seconds that has been elapsed since midnight GMT on January 1, 1970, which is referred to as the timestamp.

    Example-

    <?php
    echo time();
    ?>

    Output

    1591771649

    How to convert the timestamp value with getdate()

    PHP allows the use of getdate() function that takes the timestamp as the argument and provide the information about the date which is contained in an associative array. Below is the list of all elements that are returned by getdate() function in an array-

    Key Description Example
    seconds Represents the second of the time(0-59) 25
    minutes Represents the minutes past the hour(0-59) 30
    hours Represents the hour’s element(0-23) 14
    mday Represents the day of the month(1-31) 16
    wday Represents the day of the week(0-6) 6
    mon Represents the month of the year(1-12) 10
    year Represents the year(4 digits) 2020
    yday Represents the day of the year(0-356) 40
    weekday Represents the day of the week Monday
    month Represents the month of the year February
    0 Represents the time stamp 1591771649

    Example

    <?php
    $date_val = getdate();
    foreach ( $date_val as $date => $var ){
    echo "$date = $var";
    echo "<br>"; }
    ?>

    Output

    seconds = 19
    minutes = 2
    hours = 7
    mday = 10
    wday = 3
    mon = 6
    year = 2020
    yday = 161
    weekday = Wednesday
    month = June
    0 = 1591772539

    How to convert a timestamp with date() function

    PHP allows you to format the date using the date() function. This function can be provided with two parameters, one is the format string and other is the timestamp. If the timestamp is not provided then the function will use the current date and time for evaluation.

    date(format, timestamp)

    Below is the list of format elements that can be used within the date function-

    Key Description Example
    a Represents the ‘am’ or ‘pm’ in lowercase am
    A Represents the ‘AM’ or ‘PM’ in uppercase PM
    d Represents the day of the month 03
    D Represents the day of the week(3 letters) Wed
    F Represents the month name February
    h Represents the hour(12-hour format), leading zeros 04
    H Represents the hour(24-hour format), leading zeros 18
    g Represents the hour(12-hour format), no-leading zeros 12
    G Represents the hour(24-hour format), no-leading zeros 18
    i Represents the minutes(0-59) 26
    j Represents the day of the month with no leading zeros 20
    l Represents the day of the week Monday
    L Represents the leap year (1- yes, 0-no) 0
    m Represents the month of the year in number with leading zeros 2
    M Represents the month of the year (3 letters) Feb
    r Represents the RFC 2822 formatted date Thu, 21 Dec 2020 16:01:07 +0200
    n Represents the month of the year with no leading zeros 4
    s Represents the second of the hour 27
    U Represents the timestamp 1591772539
    y Represents the year(two-digit) 04
    Y Represents the year in four digits 2020
    z Represents the day of the year(0-365) 280
    Z Represents the offset in seconds from GMT +3

    Example

    <?php
    print date("m/d/y G.i:s<br>", time());
    echo "<br>";
    print "Today's Date is ";
    print date("j of F Y, a\t g.i a", time());
    ?>

    Output

    06/10/20 7.19:59
    Today's Date is 10 2020f June 2020, at 7.19 am

    People are also reading: