Faster PHP Cloud Hosting

Wondering what are PHP Timestamp ? how to use PHP Timestamp ? If yes, welcome to my post this post is for you.

What is a PHP Timestamp

Basically A PHP Timestamp is Unix Timestamp which is the total number of seconds from the Unix Epoch i.e from 1st January 1970 GMT.

Technically It will not change no matter where you live on the globe but if you set a Timezone for your system then the timestamp will change according to it.

Why And Where To Use PHP Timestamp

Timestamps are really useful when creating applications that include functionality like tracking , sorting, etc which is based on time.

Have you seen some modern ajax loading websites ? These kind of sites might also be using timestamp to identify and load the correct data on the users screen.

Few Useful Functions For Working With PHP Timestamps PHP

Now we will discuss few useful PHP functions for working with date and time.

The PHP time() function

The PHP time() function returns the current time in the form of timestamp i.e the total number of seconds from the Unix Epoch (1 January 1970 00:00:00).

Syntax :-

<?php echo $t = time(); // this will print the current timestamp ?>

The PHP date() function

The PHP date() function formats local date and time and returns the formatted string in the specified format.

Syntax :-

<?php date(format,timestamp); ?>

Example :-

<?php echo date("dd-mm-yyyy") ; ?>

The PHP strtotime() function

What if you are writing a php script and you need to get the timestamp of next Monday or of a particular date like 1 December 2022.

Its really tough to manually write the code to find the number of seconds to add or subtract from the current timestamp to get the exact timestamp. In such case strtotime() would be really useful what it does is that it parses English textual date time into Unix timestamp.

Syntax :-

<?php $t = strtotime(textual_datetime); ?>

Example :-


echo strtotime(""now") ; // Prints the current unix timestamp

echo strtotime("+ 1 week "); // Prints the timestamp after after one week

echo strtotime("31 December 2020") ; // Prints the timestamp of 31 December 2020

echo strtotime("next Sunday") ; // Prints the timestamp of next Sunday

echo strtotime("last Tuesday") ; // Prints the timestamp of last Tuesday


Real Life Examples Of Working With PHP Timestamp

You may have now got a clear idea of using what is php timestamp and how to use it. Now we will see some real life examples of working with it.

Example #1 : PHP Get Timestamp Or Get Current Timestamp

In our first Example we will see how to get current timestamp in php.


$t = time() ; // returns current timestamp


Or you can also do it like this :-



$t = strtotime("now") ; // returns current timestamp


Example #2 : PHP Convert Timestamp To date

You want to show a date on the websites but you can’t directly show the php timestamp on the website. Firstly you need to convert php timestamp into date  and then show it on the website.

Here is how to convert timestamp to date :-


$next_week = strtotime("+ 1 week"); // Getting timestamp of next week

$date = date("",$next_week); // Returns the date converted from timestamp

echo $date ; // Prints the converted date from timestamp


Example #3 : PHP Convert Date To Timestamp

When you want to store a date somewhere or let’s say in the database its always better to save it in the form of a timestamp as it will help you further when you have to play with the stored time.

Its Pretty easy to convert date to timestamp in php.


$date  = "1 December 2030" ; // This date can be any English textual date

$t = strtotime($date); // Convert Date To Timestamp

echo $t ; // Prints the converted timestamp


Limitation Of PHP Timestamp

The Year 2038 Problem

The Year 2038 problem is a very big issue for the all the computer programs which store time values as a signed 32-bit integer, the size of the integer of the number of seconds of since 1 January 1970 00:00:00 (Unix Epoch) i.e the Unix timestamp exceeds the 32-bit integer size. Read More on Wikipedia

I hope by now you may have become a pro in using php timestamp . In case if you didn’t understand anything or have any doubts don’t hesitate to ask me in the comments section below.

My Few other posts which you might like to read :-

And if you liked the post don’t forget to like it and do share it with your other web developer friends.