Close Menu
    Facebook X (Twitter) Instagram
    Oixiesoft
    • Home
    • Services
      • WordPress Malware Removal
      • Fix WordPress Errors
      • WordPress Website Development
    • Articles
    • Contact
    Oixiesoft
    Home»Tutorial»PHP»PHP Date() and Time() Function
    PHP

    PHP Date() and Time() Function

    Editorial StaffBy Editorial StaffUpdated:May 4, 2022No Comments4 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Dates are so much part of daily life that it evolves easily to work with them without assuming. PHP even supports effective tools for date arithmetic that make manipulating dates easy.

    The PHP time() Function

    PHP’s time() function is used to get all the details that we require about the current date and time. It needs no arguments but returns an integer.

    The integer returned by time() means the number of seconds elapsed since midnight GMT on January 1, 1970. This point is the UNIX epoch

    Example

    <?php
    print time();
    ?>
    

    Output

    The result will be here −

    1651657385

    This is something complex to understand. But PHP offers superior functions to convert a timestamp for human readability.
    We introduce date() function here.

    The PHP Date() Function

    The PHP date() function formats a timestamp to a human-readable date and time.

    Syntax

    date(format,timestamp)

    The date() optionally gets a timestamp if omitted then the current date and time will be used. Any additional data you contain in the format string passed to date() will be formed in the return value.

    Example

    <h2>PHP Get Current Date</h2>
    <?php
    $today = date("d/m/Y");
    echo "Today is " .$today;
    ?>
    

    Output

    The result will be here −

    PHP Get Current Date

    Today is 04/05/2022

    Get a Date and Time

    The necessary format parameter of the date() function defines how to format the date (or time).
    Here are some the date and time interconnected formatting signs that are generally used in format string:

    Format Description Example
    a ‘am’ or ‘pm’ lowercase pm
    A ‘AM’ or ‘PM’ uppercase PM
    d Day of the month, a number with leading zeroes 20
    D Day of week (three letters) Thu
    F Month name January
    h Hour (12-hour format – leading zeroes) 12
    H Hour (24-hour format – leading zeroes) 22
    g Hour (12-hour format – no leading zeroes) 12
    G Hour (24-hour format – no leading zeroes) 22
    i Minutes ( 0 – 59 ) 23
    j Day of the month (no leading zeroes 20
    l (Lower ‘L’) Day of the week Thursday
    L Leap year (‘1’ for yes, ‘0’ for no) 1
    m The month of the year (number – leading zeroes) 1
    M The month of the year (three letters) May
    r The RFC 2822 formatted date Wed, 04 May 2022 11:05:29 +0000
    n The month of the year (number – no leading zeroes) 2
    s Seconds of hour 20
    U Timestamp 948372444
    y Year (two digits) 05
    Y Year (four digits) 2022
    z Day of year (0 – 365) 123
    Z Offset in seconds from GMT +5

    Try out some of the following example:

    <!DOCTYPE html>
    <html>
    <body>
    <h2>PHP Get Current Date and Time</h2>
    <?php
    echo date("h:i:s") . "<br>";
    echo "Today is " .date("F d, Y h:i:s A"). "<br>";
    echo date("h:i a");
    ?>
    
    </body>
    </html>
    

    Output

    The result will be here −

    PHP Get Current Date and Time

    11:14:04
    Today is: May 04, 2022 11:14:04 AM
    11:14 am

    The PHP mktime() Function

    The mktime() function is applied to convert the timestamp based on a specific date and time. When the date and time are not given, the timestamp for the current date and time is returned.

    Syntax for mktime()

    mktime(hour,minute,second,month,day,year,is_dst)

    Example

    <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    // Create the timestamp for a 3:20:12 pm on May 5, 2022
    echo mktime(15, 20, 12, 5, 5, 2022);
    ?>
    
    </body>
    </html>
    

    Output

    The result will be here −

    1651764012

    Others Examples of mktime() Function

    Example

    <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    $tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
    echo "Tomorrow is ".date("Y/m/d", $tomorrow);
    ?>
    </body>
    </html>
    

    Example

    <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    // Get the weekday name..
    echo date('l', mktime(0, 0, 0, 5, 7, 2022));
    ?>
    </body>
    </html>
    
    Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Tumblr Email
    Editorial Staff

    Related Posts

    CodeIgniter vs Laravel: A Detailed Side-by-Side Comparison

    Python Arrays

    Python Lambda

    PHP
    • PHP Comments
    • PHP if else Statements
    • The PHP switch Statement
    • PHP Functions
    • PHP for Loop
    • PHP Arrays
    • PHP Include Files
    • PHP Date() and Time() Function
    • PHP Forms Tutorial
    Services
    • Web Development
    • Mobile Application Development
    • WordPress Malware Removal Service
    • Website Design
    • WordPress Development
    • Magento Development
    • Shopify Development
    • SEO Services
    Blog
    • How to Fix the Error Establishing a Database Connection
    • Ping List WordPress
    • How To Fix Japanese Keyword Hack
    • How to remove Malware from WordPress
    Hire Developers
    • Hire WordPress Developer
    • Hire Shopify Developer
    Contact Info
    • Oixiesoft Technologies
      A-40, Block A, I thum Tower, Sector 62, Noida
    • [email protected]
    • Privacy Policy
    • About Us
    • Contact Us
    © 2025 OixieSoft Technologies

    Type above and press Enter to search. Press Esc to cancel.