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 and Time in PHP

    How to Get Current Date and Time in PHP

    By 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.

    Advertisement

    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.

    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:

    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.

    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

    Related Posts

    Installing PHP on Pop!_OS

    How to Install PHP 8.x on Pop!_OS

    Managing Dependencies with Composer: A Beginner’s Guide

    10 Simple Ways to Speed Up Your WordPress Website

    10 Simple Ways to Speed Up Your WordPress Website

    View 2 Comments

    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

    Advertisement
    Recent Posts
    • Error: EACCES: permission denied, scandir (Resolved)
    • How To Install Python 3.11 on Ubuntu 22.04 / 20.04
    • How to Install Python 3.11 on Amazon Linux 2
    • An Introduction to the “./configure” Command: Compiling Source Code in Linux
    • How to Install PHP 8.x on Pop!_OS
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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