Working with date() and time() in PHP October 28, 2010Posted by Tournas Dimitrios in PHP.
This article shows you how to work with dates and times in PHP. Web programs often involve an extensive amount of extraction, manipulation, comparison, and display of dates. The task gets more involved when you add MySQL to the mix. But first, let’s talk about the native time format on your server.
How Unix Time Works :
Before going into detail about how to work with dates,an introduction has to made about the basics of how POSIX-compliant Unix/Linux servers (and thus, PHP) keep time.
Midnight, January 1, 1970, is when time “began” for Unix computers. In Unix parlance, that timestamp is 0, and it’s called the epoch date.
Any date since then is calculated as the number of seconds that have elapsed between January 1, 1970, and that time. For example, as I write this at 12:00:21 on october 28 , 2010, 1,288,256,378 seconds have passed since that fabled date. Thus, the timestamp as I write this is —1288256378.
This scheme makes manipulating dates in PHP relatively easy as long as you don’t need to show the date in a readable format. For example, you can jump ahead a day by adding (60 x 60 x 24) seconds to the current timestamp or go back one hour by subtracting 3,600 seconds. Because the timestamp is just a number and computers can handle numbers with a explicit way , it is handy when comparing adding ,substracting numbers that represend the time .
Because humans don’t understand timestamps very well, whenever you want to print a date, you should use a format to convert to human readable time . PHP supports a bunch of methods to make this work easy for us .See the PHP manual .
To get the current timestamp, call the time() function with no arguments. The return value is the current timestamp on the server. This simple example prints the current time:
echo ‘The current timestamp is ‘ . time();
You can also assign the returned value of the timestamp() function to variables, like so: $time = time();
Many PHP date-related functions take a timestamp argument, and most of them use the current time if you don’t supply that parameter. For example, the date() function returns a formatted date string in a custom format (like Nov 1, 2010), by using the syntax date(“format”, timestamp). But if you don’t give it a timestamp, the default is the current time. This means that for most of the time functions, you don’t even need to include time() as a parameter if you want to work with the current timestamp.
echo date(“D:d F Y”); // returns : Thu:28 October 2010
The table below shows all the posible arguments that you can pass to the date() function , so the output can be formated to your needs .
date() Function Sample Strings :
|Date Format String||Sample Output|
|l (lowercase L)||Saturday|
|G:i:s A||5:26:01 PM|
|M-d-Y h:m:s a||Aug-22-2010 12:08:00 am|
Getting the Timestamp of a Date in the Past or Future :
Let’s explore how to determine some other timestamps close to the current time. For example, you can extract yesterday’s timestamp or, say, next Friday’s. There are two general methods for finding these values:
- Determining the timestamp based on a string
- Determining the timestamp from date values (which can be a bit trickier)
- Creating Timestamps from a String
- Creating Timestamps from Date Values
The strtotime() function can sometimes be a PHP programmer’s best friend. Just as its name suggests, strtotime() generates a timestamp from an English date-phrase, such as April 5 or Friday. These strings can be either relative to the current date or absolute. Table below outlines some of the possibilities.
|Invocation||Output (as a timestamp)|
|strtotime("Friday")||Friday at midnight|
|strtotime("+1 week Friday")||The Friday after next at midnight|
|strtotime("+1 week")||A week from right now|
|strtotime("-2 months")||Two months ago from right now|
|strtotime("October 21, 2010")||October 21, 2010, at midnight|
|strtotime("2008-10-01")||October 21, 2010, at midnight|
|strtotime("Friday 12:01 p.m.")||Friday at 12:01 PM|
|strtotime("+7 days 12:01 p.m.")||Seven days from now at 12:01 PM|
If you know the precise date and time you need, you can create a timestamp with the mktime() function. To use the mktime() function, you need the hour, the minute, the second, the number of the month, the day, and the year of the timestamp you want to create.
$future_date = mktime($hour, $minute, $second, $month, $day, $year);
Finding the Difference Between Two Dates :
You can use strtotime() to convert two dates to unix time and then calculate the number of seconds between them. From this it’s rather easy to calculate different time periods.
$date1 = "2007-03-24"; $date2 = "2010-10-28"; $diff = abs(strtotime($date2) - strtotime($date1)); $years = floor($diff / (365*60*60*24)); $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); printf("%d years, %d months, %d days\n", $years, $months, $days);