Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»PHP»How to Get Current Date and Time in PHP

    How to Get Current Date and Time in PHP

    RahulBy RahulFebruary 18, 20171 Min ReadUpdated:June 5, 2019

    You can use PHP date() function or DateTime() class to get current Date & Time in PHP. This tutorial will help you to get current date time in PHP.

    The provided results based on the timezone settings in the php.ini file. You may need to modify this setting to get date and time in the required timezone. Read this tutorial to set timezone in PHP configuration.

    Using date() Function

    PHP date() function converts the timestamp stored in computer memory in a human-readable format. The syntax of date() function is as:

     date(format, [timestamp])
    

    The below php script will return current date time in ‘Y-m-d H:i:s’ format.

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

    Result:-

    2017-02-27 08:22:26
    

    Other useful examples:

    PHP
    1
    2
    3
    4
      date('Y/m/d');           //  2017/02/27
      date('m/d/Y');           //  02/27/2017
      date('H:i:s');           //  08:22:26
      date('Y-m-d H:i:s');     //  2017-02-27 08:22:26

    Using DateTime() Class:

    The DateTime() class functions allow you to get the date and time. You can also use functions to format the date and time in many different ways.

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

    Result:-

    2017-02-27 08:22:26
    

    References:
    http://php.net/manual/en/function.date.php
    http://php.net/manual/en/intro.datetime.php

    date datetime PHP php date function php datetime class time
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow To Parse JSON in Node.js
    Next Article How To Install Go 1.17 on Debian 11/10/9

    Related Posts

    How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04

    Updated:May 9, 20223 Mins Read

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    How To Install LAMP Stack on Ubuntu 22.04 LTS

    Updated:April 20, 20225 Mins Read

    How To Setup Apache, PHP & MongoDB in Ubuntu & Debian

    Updated:October 8, 20213 Mins Read

    How To Install and Use PHP Composer on Debian 11

    Updated:February 16, 20224 Mins Read

    How To Install PHP (8.1, 7.4 & 5.6) on Debian 11

    Updated:February 16, 20224 Mins Read

    2 Comments

    1. Zeshan on June 4, 2019 1:51 pm

      WAMP: showing me time 3 hours before time. Its 18:22:39 but it is showing time part 15:22:39.

      My Local PC time is also fine and showing time by 18:22:39

      Why ?

      Reply
      • Rahul on June 5, 2019 4:21 am

        You may need to change timezone in PHP configuration:

        https://tecadmin.net/setup-timezone-in-php-configuration/

        Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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