PHP TIME STAMP and DATE function

Date and time in PHP PHP time stamp is a numeric value in seconds  between the time at present and the value at Unix Epoch (January 1 1970 00:00:00 GMT).   This time of January 1 1970 00:00:00 GMT is taken as base for all time stamp calculations.  Below is the time stamp or the number of seconds lapsed after January 1 1970 00:00:00 GMT and when this page is loaded ( or opened by you). You can refresh this page to see the current time stamp. Press the refresh button of your browser to reload this page to see the current time stamp.
Current time stamp= 1627104565 

php time stamp generator to find the time stamp of any date

To display current time stamp we have to use time() function like this .
<?Php
echo time();
?>

This line will print the value of current time stamp as displayed at the top. Time stamp is used in many PHP applications and from time stamp the current date and many different date format can be taken by using different functions in PHP. stamp.

Displaying current time

We can display current time by using time() functino. Here is the code
echo date(" H:i:s", time());
The output is here 10:59:25

JavaScript we can display real time changing clock

Show current time & date by using localtime function in PHP

Here is the current time stamp again : 1627104565
From the value above let us display the current date and time by using the date function
echo date("m/d/y",time())
Here is the output showing current date and month value using the above  timestamp.
 07/24/21 

Format for MySQL datetime field

MySQL datetime field stores date with time. Here is the format required to store data in this field.
$tdtp=date("Y-m-d  H:i:s",time());
Here is the output
2021-07-24  10:59:25
You can find detail php date today functions and php date formats in other tutorials in this section.

If you want to display the current time along with the date then change the format of the date function like this
date("d/m/y : H:i:s", time());
the out put of this will be like this :
 24/07/21 : 10:59:25
Don't forget to refresh this page to see how the value changes.

Please note that the current date and time value displayed will be affected by default time zone set in your php.ini file of the server.

If you are testing this script in your local machine then take care of time zone setting in your php.ini file is set to your time zone.

You can change your time zone setting at your script page level by using this command
date_default_timezone_set ("Asia/Calcutta");

Or ( one more example )

date_default_timezone_set ("America/Denver");

You can create two demo pages to understand concept of Time stamp.

Using html meta page refresh, create page which will reload itself at every 10 seconds and display the updated timestamp.
Try to display the timestamp in every 10 second duration without reloading the page. ( Use Ajax )

Solution: Server Clock using Ajax

Date Picker by using Calendar

By using JavaScript we can display current month calendar and user can select a date from the calendar to input to a textbox. User can browse to different months of the calendar and select different dates.

User can also select two dates indicating Start date and end date.

Date picker by using JQuery

Jquery widget for calendar can be easily integrated to any website, you can set a range, disable dates and apply many other requirements easily.

Date Picker using JQuery



Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com






    biswa

    07-01-2009

    Nice tutorial.
    Alex

    14-03-2009

    thanks, this tutorial really helped :)
    wilfer

    16-03-2009

    I found what I have been looking for
    Tanmoy

    30-04-2009

    really nice tutorial
    biraj

    27-05-2009

    Helped me a lot :)
    Amol Bhavsar

    08-06-2009

    Nice Article
    sunil

    28-06-2009

    thanks,excellent tutorial.
    neha

    02-07-2009

    thanks it help me alot:)
    wpbweb

    20-08-2009

    Thanks this is the only code that actully displayed the current time.
    ganesh

    31-08-2009

    nice tutorial...
    sresti

    08-11-2009

    excellent tutorial and easy to read and easy to understand and more flexible to even beginers also..
    Jarnail

    24-11-2009

    Thanks, Such a good tutorials. really help to convert time to date format
    drmd - pmai.tk

    31-12-2009

    pretty interesting tutorial but i have a nota .. while # echo time() # displays current time-stamp, # echo date("m/d/y",time()) # displays current time in the SERVER, e.g. her it s GMT - 6 so to display a GMT current time we have to add a sum of 6*60*60 to the time-stamp ..
    burnquiss

    29-01-2010

    how to save my date in to my sql please, give tutor tq
    manohar yadav

    15-02-2010

    its realy very helpful for me..if is there any rating then i rate it to 80% fro javascript.
    harold

    02-03-2010

    nice one
    Rahul

    30-03-2010

    How to convert 3 text box value in a one variable in PHP
    Ankit Tomar

    06-04-2010

    Thanx for the tute specially for Time zone Asia/calcutta
    amit chaudhary

    22-06-2010

    thanx for this nice tutorial,it is very helpful for me..
    Pritesh

    30-06-2010

    yes its a very good tutoril all problems all easy solved thank you
    Jahtex

    29-08-2010

    oh.. what a great tut... the mention of the php build in function date_default_timezone_set(); is a great revelation to my coding life.. once again many thanks to the writer...
    Masees Skenderian

    18-09-2010

    iam using function: date("d/m/y : H:i:s", time()) how do i display the time in a 12 hours clock not a 24 hours clock?
    smo

    29-09-2010

    Use small ( lower case ) h in place of upper case H. You can read more on date formats.
    tiffin

    02-11-2010

    i am using function echo date("g:i"); But the time is early 4 hours from my pc time.How can i synchronize the time so that its the same as my pc time?
    Amit

    06-12-2010

    How to use time stamp in cookies
    Ernest Darling

    10-01-2011

    You are a life saver buddy but one more thing, How do I store a Timestamp into mysql database so it can later be retrieved as Timestamp?
    Anantha Narayanan

    29-04-2011

    Hi, This was very helpful, I have a doubt in mysql timezones and time-format, see i have a table in DB which has 2 important column which is location and timestamp, in which location can be anything, but i want the timestamp to be same format as it generated, since we have customers from different timezones, which i want the timestamp to be saved as it is, say if GMT it should saved as GMT, if it is pacific , it has to saved as PACIFIC, Kinldy help me with your suggestions
    Chris Kefel

    22-05-2011

    As a newbie php-user, this tutorial helped me a lot. Thanks
    tim

    02-01-2012

    Thanks for the tutorial. My question is, if i have 2 events . How do l set the code to show the actual event each of the 2 events occured?
    malekHaron

    10-05-2012

    thanks a lot bro! its really worked and its great! you sir, should be in the avengers! ;)
    Jace

    25-05-2012

    Great tutorial, Thanks
    iqbal iqbal

    01-09-2012

    great one
    amith

    20-09-2012

    tnx bro... its really worked.....
    PREM

    19-10-2012

    Its very best way to practical success
    Victor Olaru

    15-12-2012

    Works perfectly, Thank you very much, Best regards
    rubab

    03-04-2013

    <?= date("D M d Y h:i:s a",time()); ?>
    Every thing is perfect.the thing i want is now to display the time zone of server.Can anyone help me out.
    its out put is:
    Tue Apr 02 2013 07:32:06 pm (i want time zone to be displayed with this output)
    smo1234

    16-07-2013

    You can use date_default_timezone_get() function to display time zone of the server
    Sagar

    08-01-2014

    Thx buddy.... The thing that I was looking for since long time.... Thx much...
    Shaan

    04-06-2014

    How to add number of months to an current date
    Bryan

    12-07-2014

    wow, thank you been trying to find out how to do this for ages, thanks a lot!
    dnyanesh

    24-12-2014

    i want to convert timestamp in to seconds like below convert from date function.
    anybody know any function to convert timestamps in seconds .
    Thanks in advance !!!
    var ctimeInSec = new Date();
    alert(ctimeInSec.getUTCSeconds() );
    mysql

    06-07-2015

    how can i display the current dynamic time without refreshing the current page
    smo

    09-07-2015

    One way is using JavaScript to display clock from client side ( not server time ), you can check the tutorial on <a href=../javascript_tutorial/clock.php>dynamic clock</a>. There are links at end of the tutorial to show you how to collect server side time and display.
    cedric

    02-05-2016

    You just solved a problem that i spent 3 days searching everywhere. Thanks a lot!
    DAVISON PHIRI

    08-11-2016

    thinks for the time and date function, this function is really working but what am looking for is that: certain div part should refresh it safe after certain interval of the time.
    demo

    15-11-2016

    You just solved a problem that i spent 3 days searching everywhere. Thanks a lot!

    Post your comments , suggestion , error , requirements etc here .




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer