Date manipulation in PHP
Modifying the date
Often we will want to know what the time will be in 6 hours time, what the date was 35 days ago or how
many seconds it has been since you last played Quake3. We've already seen how the mktime() function
can be used to generate a unix timestamp from individual date and time elements. If we wanted to
create a unix timestamp out of the current date and time, how would we go about it ? A bit of a
pointless exercise, but it will help to illustrate what we will be doing later.
As we have seen, mktime() takes the following arguments: hour, minute, second, month, day, year. And
if you recall from way back in section 2, we saw that the getdate() function can get all those bits
and pieces for us.
<?php // get the current timestamp into an array $timestamp = time (); echo $timestamp ; echo '<p>' ; $date_time_array = getdate ( $timestamp ); // use mktime to recreate the unix timestamp $timestamp = mktime ( $date_time_array [ 'hours' ], $date_time_array [ 'minutes' ], $date_time_array [ 'seconds' ], $date_time_array [ 'mon' ], $date_time_array [ 'mday' ], $date_time_array [ 'year' ] ); echo $timestamp ; ?>
Looks a little bit confusing, so what I'll do is add in a couple of variables so that things make a little bit more sense.
<?php // get the current timestamp into an array $timestamp = time (); echo $timestamp ; echo '<p>' ; $date_time_array = getdate ( $timestamp ); $hours = $date_time_array [ 'hours' ]; $minutes = $date_time_array [ 'minutes' ]; $seconds = $date_time_array [ 'seconds' ]; $month = $date_time_array [ 'mon' ]; $day = $date_time_array [ 'mday' ]; $year = $date_time_array [ 'year' ]; // use mktime to recreate the unix timestamp $timestamp = mktime ( $hours , $minutes , $seconds , $month , $day , $year ); echo $timestamp ; ?>
Now that we've taken the info out of the array that was created by getdate() and into
appropriately named variables, the code is a bit easier to read and understand. The great
thing now is that if we wanted to add 19 hours to the current date, instead of feeding
$hours to mktime(), we can just feed it $hours +19. mktime() will do the automatic
rollover to the next day for us!
<?php // get the current timestamp into an array $timestamp = time (); echo strftime ( '%Hh%M %A %d %b' , $timestamp ); echo '<p>' ; $date_time_array = getdate ( $timestamp ); $hours = $date_time_array [ 'hours' ]; $minutes = $date_time_array [ 'minutes' ]; $seconds = $date_time_array [ 'seconds' ]; $month = $date_time_array [ 'mon' ]; $day = $date_time_array [ 'mday' ]; $year = $date_time_array [ 'year' ]; // use mktime to recreate the unix timestamp // adding 19 hours to $hours $timestamp = mktime ( $hours + 19 , $minutes , $seconds , $month , $day , $year ); echo strftime ( '%Hh%M %A %d %b' , $timestamp ); echo '<br>~E after adding 19 hours' ; ?>
Running this now gives me:
14h58 Saturday 03 Jun
09h58 Sunday 04 Jun
~E after adding 19 hours
Subtracting time is done in exactly the same way - just subtract the amount of time you want from the relevant variable.
Working out the difference between two times is also pretty straightforward. All you need to
do is have both converted into unix timestamps and you just subtract the one from the other.
The difference will be the difference between the two times in seconds. Some quick arithmetic
can convert the seconds into days, hours, minutes and seconds.
[ Next Page ] [Page 1] [Page 2] [Page 3] [Page 4] [Page 5]
Comments:
RE: day addtion and substration Rasika Somathilaka 10/20/08 02:28
Daylight save time Venoel 10/26/07 06:54
RE: datediff mamun 10/10/07 23:23
RE: Add 7 days to an existing date jprakash 09/18/07 02:08
RE: Inserting dates into mysql Hussain 07/19/07 01:31
Inserting dates into mysql Tams 07/12/07 14:55
Comparing dates in Unix stamp Nour B 05/13/07 07:59
RE: Converting 2001-1-01 to 1-1-2001 TheButler 02/14/06 11:46
Function to convert dates to and from Mysql Chris McKee 10/21/05 00:02
RE: Add seconds. BrianD 07/01/05 20:20
datediff kiwami 06/09/05 02:49
RE: start date and end date of the week arup kumar mitra 04/14/05 09:23
Add 7 days to an existing date Rika 04/13/05 07:47
Day from a date in the past Jacob 04/07/05 06:07
Inserting todays date into a table Mike G 03/29/05 10:05
add time sanera 03/21/05 03:21
date and time in php Mike 03/17/05 00:35
Add seconds. Shawn 01/24/05 15:29
Date serching Cyril 01/13/05 06:12
HELP...im really struggling to.... Raj Patel 12/28/04 19:55
Should not work properly Lluis 12/15/04 04:32
help ........ php pree 11/17/04 04:05
SOLUTION - MYSQL TO PHP DATE STAMP Paul 10/24/04 18:45
mistake in dateAdd function drew 09/13/04 23:00
RE: date diff & leap years Ahad ali 09/13/04 04:21
Find the start and end dates of a month Kumar 05/05/04 08:24
RE: start date and end date of the week Kumar 05/03/04 07:55
RE: start date and end date of the week Sunny 03/07/04 01:42
SOLUTION: MYSQL -> PHP Karl Messner 12/17/03 22:38
RE: Converting 2001-1-01 to 1-1-2001 madjack 08/08/03 19:35
Updated Quarter Code Ian 05/19/03 22:50
RE: start date and end date of the week selidori 03/17/03 10:58
Get exact Time Difference betn two dates. Vijay 02/17/03 05:16
RE: formatting date from a MySQL table PHP Bob 12/03/02 00:40
RE: Expanded Date Diff - Years, Quarter, Mont Allen 11/25/02 10:51
RE: Trying to input a date from a form text input mona vaishnav 11/19/02 01:19
RE: using LIKE with wildcards joris 11/17/02 21:22
RE: formatting date from a Ms-SQL table Fred Shen 11/11/02 18:11
RE: strtotime doesn't work Dan 11/08/02 09:35
DONT NEED THIS, STRTOTIME Dan 11/08/02 09:34
good work but..... Yassine 11/01/02 04:30
Conversion to readable date Kevin 10/31/02 09:10
Expanded Date Diff - Years, Quarter, Month geoff whittington 10/17/02 12:42
upload file liyla 10/13/02 01:27
Date and time sathappan 10/04/02 02:14
solution converting data from mysql to php Tom s 09/24/02 09:00
start date and end date of the week rakesh 09/22/02 06:19
Epoch conversion TrueFaith 09/19/02 03:22
Get all timstamps for a whole day? Arash 09/02/02 17:18
How to get date from day of year Amanda 09/01/02 07:22
RE: formatting date from a Ms-SQL table Anitha 08/28/02 05:44
Thanks a lot! Goblin 08/27/02 08:26
RE: date comparison Anurag 08/27/02 06:33
Unix Timestamp Richard Dorsett 08/23/02 11:06
using LIKE with wildcards smitam 08/22/02 01:19
date comparison smita 08/21/02 22:13
RE: Date Handling Kim Gan 08/06/02 13:20
Display format date fetching date from oracle Haseeb ur Rehman 07/23/02 01:51
server side link timer script rogie 07/06/02 10:09
RE: datediff process noshee 07/02/02 06:31
DateDiff: Dates before 1970 Seth 06/19/02 16:16
Bug in DateAdd Alberto Sylvester 06/18/02 11:28
need to flag new listings... Fritz 06/12/02 20:46
dynamic calendar rajkavi 05/30/02 06:19
Cannot redeclare datediff() dsamsom 05/29/02 13:35
Restriction of range date Dewi 05/23/02 05:35
Thanks You! Shelagh 05/09/02 07:24
Thank You, Thank You, Thank You!!! Bob Langdon 05/05/02 17:31
Format a MySQL timestamp ?? Michael 04/24/02 04:31
Many thanks for this article David Court 04/19/02 05:19
Great Article Ryan Schwiebert 04/16/02 11:27
date of next weekend Holger 04/05/02 09:00
Adding these functions to the distribution Malcolm Davis 04/04/02 08:38
RE: get Day from any given date rodney 04/02/02 08:37
inserting count according to latest date. diksha 04/01/02 01:16
get Day from any given date vipul patel 03/21/02 11:51
Want to get date and time of subdirectories Pakiya 03/20/02 03:11
strtotime doesn't work Alex 03/18/02 04:19
RE: Basic university project mixi de France 03/03/02 15:32
RE: friendly date scripot port-est 02/23/02 15:06
friendly date scripot e johnstone 02/18/02 10:55
tree view yana 02/18/02 03:53
RE: Trying to input a date from a form text input jdc44 02/17/02 21:04
RE: Trying to input a date from a form text input jdc44 02/17/02 20:51
I am getting a wrong date when using date() Kishore Kadiyala 02/16/02 00:49
HELP URGENT !!! Compare date and time Daven 01/29/02 06:38
RE: I want time in my page stewartg 01/24/02 18:15
Dates and Times with PHP and MySQL stewartg 01/24/02 03:09
RE: time zone of visitor stewartg 01/24/02 03:05
RE: time zone of visitor stewartg 01/24/02 02:40
RE: Trying to input a date from a form text input stewartg 01/24/02 02:25
RE: How to add date stewartg 01/24/02 02:23
RE: Basic university project stewartg 01/24/02 02:16
RE: time spent by a visitor in a page stewartg 01/24/02 02:12
RE: epoch seconds stewartg 01/24/02 02:01
simple date calculation (days difference) stewartg 01/24/02 01:58
RE: I want time in my page Bob 01/20/02 10:48
RE: I want time in my page Deflatarat 01/18/02 17:51
RE: no of days in month Deflatarat 01/18/02 17:46
RE: day addtion and substration Deflatarat 01/18/02 17:38
how to put a file into mysql database Sorin 01/17/02 11:15
Translate date into other languages! Copycat 12/30/01 12:41
Translate date into other languages! Copycat 12/30/01 12:31
RE: day addtion and substration Jim Gantes 12/16/01 05:15
day addtion and substration pant 12/08/01 06:25
formatting date in ODBC Mike Page 12/06/01 10:57
RE: Converting 2001-1-01 to 1-1-2001 PackWolf 12/04/01 20:18
RE: Converting 2001-1-01 to 1-1-2001 Axel Brown 11/30/01 01:06
Changing Dynamic Date format ofwofw 11/29/01 10:23
RE: Converting 2001-1-01 to 1-1-2001 hugo 11/26/01 13:16
RE: Converting 2001-1-01 to 1-1-2001 Protect 11/24/01 22:31
RE: Converting 2001-1-01 to 1-1-2001 beca44 11/21/01 08:31
RE: Converting 2001-1-01 to 1-1-2001 spiri 11/13/01 02:25
RE: I want time in my page Adam 11/04/01 23:53
RE: Converting 2001-1-01 to 1-1-2001 Greg 11/04/01 01:19
Time zone manipulation Dougal Campbell 10/31/01 15:06
Converting 2001-1-01 to 1-1-2001 madjack 10/31/01 13:11
no of days in month himanshu 10/30/01 05:01
RE: User Inserting Date Alex 10/14/01 20:11
RE: Date difference Bjørn Rydén 10/12/01 06:34
User Inserting Date Angela 09/27/01 10:09
Date difference Rahil Mumtaz 09/14/01 06:24
process form atheer 09/05/01 19:50
I want time in my page Greenpis 08/28/01 19:18
RE: bcmath, why needed? bal 08/18/01 09:30
I wanna just calculate the age Edemilson Lima 08/14/01 15:03
Date variable Steve 08/14/01 08:47
how to dateDiff months? irf 08/12/01 13:00
RE: formatting date from a MySQL table Emanweb 08/12/01 03:21
date diff & leap years irf 08/10/01 02:17
This function doesn't work. Michael Harris 08/04/01 15:29
RE: time zone of visitor Benji 07/31/01 08:23
RE: formatting date from a MySQL table loudi 07/12/01 11:52
Trying to input a date from a form text input Don 07/11/01 14:10
How to add date Ramya 07/11/01 00:51
Doesn't help me Frank Hilliard 07/09/01 09:07
why "space" add befor month number gosling 07/02/01 21:40
RE: Apostrophe from a form Diablo7227 06/08/01 12:56
formatting date from a MySQL table php dummy 06/05/01 16:52
RE: Apostrophe from a form Leed 05/25/01 01:48
Apostrophe from a form Rick Templin 04/07/01 17:06
I NEED HELP !!!, I'M A MORON ! HitthaGspt 03/11/01 21:12
RE: bcmath, why needed? Nightslave 03/01/01 01:09
RE: Basic university project Matt M. 02/15/01 08:41
Basic university project Matthew Hanson 02/08/01 08:24
thanks Doug Korthof 02/04/01 18:12
RE: time zone of visitor Jon Robins 01/26/01 03:59
RE: time spent by a visitor in a page kumar 01/12/01 18:55
time zone of visitor John Derrickson 01/03/01 14:34
How do I get time difference in VB or Java Sc Mahidhar 12/14/00 08:25
RE: datediff process Rafay Mohd 12/11/00 10:34
RE: Remote machine date Rafay Mohd 12/08/00 07:44
RE: datediff process Johannes 11/29/00 09:48
Remote machine date R.Sridhar 11/09/00 12:39
bcmath, why needed? Paul Slootman 10/05/00 09:48
epoch seconds Wendy Roseberry 09/21/00 18:44
For Nelson :: The first part of a solution clee.pf.mc 07/04/00 18:04
Time error Nelson Acero Fino 07/04/00 09:04
time diference error Nelson Acero Fino 07/01/00 19:23
datediff process Nelson Acero Fino 07/01/00 19:22
If you are looking for help, please post on the appropriate forum here . Your questions will be answered much more quickly.