PHP Date and Time

No votes yet.
Please wait...

Hello guys,

As we know date and time are used in most of the functions of a project, in some we need to sort data by the creation date and in others we may  have to show date with data,there can be infinite usage of dates in different formats while developing a program.

So today PHP Hurdles is presenting a tutorial on PHP DATE and TIME where we will learn to extract or format the date and time in PHP.

System stores dates and times in UNIX Timestamp, which measures time as a number of seconds, which is an impractical format for humans to read, PHP uses a variety of functions to convert a timestamp into human readable format.

So lets start with some of the functions to convert Unix timestamp to human readable date and time :
The PHP Date() Function

The syntax of the PHP date() function can be given with.

1
date(format, timestamp)

This Date() function is used to convert timestamp into human readable date where format can be Y-m-d or Y/m/d etc

We can use format according to the format of the result we want
we can use date-related  formatting characters which are used in the format string:
d – shows the day of the month;
D – shows day of the week in text (Mon to Sun)
m – shows the month in numbers
M – shows month in text
y – shows year in two digits
Y – shows year in four digits

These parts of the string can be separated using characters like ‘ – ‘ , ‘ / ‘ and ‘ . ‘

Now we will learn
The PHP time() Function

The time() function is used to get the current time as a Unix timestamp.

1
2
$timestamp = time();
echo($timestamp);

This timestamp can be converted into human readable format using date() format

1
2
<?php $timestamp = 1394003958;
 echo(date("F d, Y h:i:s", $timestamp)); ?>

Moving ahead
we will learn How to get the day of the week from a Unix timestamp

Given the following timestring:
Let say we have a time string $str = ‘2017-07-06’
and we have to get the day of the week according to this string
strtotime can be used to parse a time string, and resulting timestamp can be passed to getdate (or use date to format our time).

1
2
3
4
<?php $str = "2017-07-06";
$time = strtotime($str);
 // You can now use date() functions with $time, like $weekday = date("d", $time);
 // Wednesday or whatever date it is ?>

This will give the output Thursday.
d  gives Day of the week as output from timestamp by which we can get day of week.

So guys these are some functions of DATE and TIME in PHP which we can use in our coding.

This tutorial will help our new developers to solve issues related to date and time in projects.

Hope PHP Hurdles is doing good in the field of tutorials where our only motive is to help our new developers to solve those issues which we have faced at starting of our career.we are not here to compete with anybody, the only thing we want is to spread knowledge about PHP and as learners we learn every day and share those things with our fellows.

So please comment us and mail us regarding topics in which you guys are having issues, our team will try to help you.
Because Hurdles aren’t really Hurdles

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *