Fri, 17 May 2024


PHP Date and Time Functions

By: Peter, NetArt Media
Wed, 1 August 2018
PHP Date and Time Functions

It goes without saying that having the ability to display, work with and manipulate date and time would be a relevant part of web developing with PHP. While there are many, many options available within the PHP language for doing so, not all are needed. Even with just a few simple functions we can create scenarios that are used all over the world on millions of websites.

We can first start with a simple returning of the current time, based on a defined, default timezone.


I reside within the Los Angeles timezone, so it's the one I use for my purposes. 'Date' must be used in conjunction with 'time' in order for us to get the current time. For myself, the current time on my development server is '17:54:26'. This isn't very legible though, in my opinion, so I change the parameters of the code to return something more friendly.


5:55 pm

The result you see above would be a minute or two later, of course. Much easier to read, correct? I think so.

Now let's display the full date, with the legible time.


The result for me using this code block is...

Tuesday, May 31, 2016, 5:55 pm

In the event we want to work with date and time so we can perhaps carry out mathematical equations, we have to use the 'strtotime' function. This turns a full date and time into a long number, which we need in order to carry out the next few examples I am about to present.


The result for the above code block on my server, at this current time, would be...

1464743160

Don't let the number scare you away. ;-) As I mentioned, we need it in order to carry out the next few examples.


In the above code block I added 24 hours to the current time, using 'strtotime' beforehand. This gives us 2 strings of numbers to sum up. Easy, but something that could be taken to many different levels to create a pretty cool script. The result for the above code on my server is...

June 1, 2016, 6:55 pm

Now we can create a condition, find out what day of the week it is by using the 'l' parameter and return an appropriate message if today is the day defined.


Because today is Tuesday for me, this is the message returned in my browser...

Yes, today is Tuesday.

Now we can take things to an even greater level, finding out what day it was yesterday, today and tomorrow. I like this code and have used something similar in the past for creating calendars for clients.


The 'l' parameter within a 'date' clause returns the day of the week. If I want to return today's day, I have to use it, of course.

I then created variables for today, yesterday and tomorrow with 'strtotime', carried out the subtraction and addition necessary to add and minus 24 hours and finally, re-defined yesterday and tomorrow to be returned as days of the week. Pretty cool, huh? I think so. Below is the result in my browser.

Today is Tuesday.
Yesterday was Monday.
Tomorrow is Wednesday.

This is a longer way of going about things, as turning the days of the week into an array and using a foreach statement would take up less code and be faster. However, I wanted to give you an idea of how this kind of thing can be carried out.

PHP date and time functions are great to play around with and can produce amazing results. They are such an integral part of the web development process and could never be underestimated. Have fun with them and see what you can come up with yourself.


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox