Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»PHP»How to Get Current Date & Time in PHP

    How to Get Current Date & Time in PHP

    By RahulAugust 7, 20182 Mins Read

    Question – How to Get Current Date Time with PHP? How do I get current date and time in PHP script? PHP Script to get current date and time in Y-m-d H:i:s format?

    Advertisement

    You can use date() function to get current Date and Time in any PHP version. A DateTime PHP class is also available in PHP > 5.2 to get Date and Time. Also find the example to get date and time in JavaScript

    PHP Date and Time

    Print the current date and time using date() function. It will use the default timezone configured in php.ini.

    1
    2
    3
    <?php
    echo date('Y-m-d H:i:s');
    ?>

    Print the current date time using DateTime() class. It will also use the default timezone.

    1
    2
    3
    4
    <?php
    $datetime = new DateTime();
    echo $datetime->format('Y-m-d H:i:s');
    ?>

    PHP Date and Time with Timezone:

    You can also define the specific timezone for your Application. Now PHP will not use the system defined timezone.

    1
    2
    3
    4
    5
    6
    <?php
    date_default_timezone_set('UTC');
     
    $datetime = new DateTime();
    echo $datetime->format('Y-m-d H:i:s');
    ?>

    Storing date and time UTC format is the best practice. UTC is refereed as Universal Time Coordinated. It is also known as “Z time” or “Zulu Time”.

    Convert Date Time Timezone

    For example, you have stored date (2018-01-11 01:17:23) in UTC timezone in your database. Now convert this DateTime to EST timezone.

    1
    2
    3
    4
    5
    6
    <?php
    $date = new DateTime('2011-11-10 20:17:23', new DateTimeZone('UTC'));
     
    $date->setTimezone(new DateTimeZone('EST'));
    echo $date->format('Y-m-d H:i:s');
    ?>

    After executing the above command, you will find the results like below.

    2018-01-11 01:17:23
    

    date datetime PHP time timezone
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Prevent SQL-injection in PHP using Prepared Statements

    Preventing SQL injection attacks with prepared statements in MySQL

    PHP Arrays: A Beginner’s Guide

    View 2 Comments

    2 Comments

    1. Mohit Sharma on August 20, 2021 12:01 pm

      date is correct but time is not correct

      Reply
    2. Tara on January 17, 2018 3:14 pm

      It helps me.. Thanks

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    • dd Command in Linux (Syntax, Options and Use Cases)
    • Iptables: Common Firewall Rules and Commands
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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