UNIX Systems Administrator (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume

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
'&lt;p&gt;';
$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
'&lt;p&gt;';
$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
'&lt;p&gt;';
$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
'&lt;br&gt;~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 substrationRasika Somathilaka10/20/08 02:28
Daylight save timeVenoel10/26/07 06:54
RE: datediffmamun10/10/07 23:23
RE: Add 7 days to an existing datejprakash09/18/07 02:08
RE: Inserting dates into mysqlHussain07/19/07 01:31
Inserting dates into mysqlTams07/12/07 14:55
Comparing dates in Unix stampNour B05/13/07 07:59
RE: Converting 2001-1-01 to 1-1-2001TheButler02/14/06 11:46
Function to convert dates to and from MysqlChris McKee10/21/05 00:02
RE: Add seconds.BrianD07/01/05 20:20
datediffkiwami06/09/05 02:49
RE: start date and end date of the weekarup kumar mitra04/14/05 09:23
Add 7 days to an existing dateRika04/13/05 07:47
Day from a date in the pastJacob04/07/05 06:07
Inserting todays date into a tableMike G03/29/05 10:05
add time sanera03/21/05 03:21
date and time in phpMike03/17/05 00:35
Add seconds.Shawn01/24/05 15:29
Date serchingCyril01/13/05 06:12
HELP...im really struggling to....Raj Patel12/28/04 19:55
Should not work properlyLluis12/15/04 04:32
help ........ phppree11/17/04 04:05
SOLUTION - MYSQL TO PHP DATE STAMPPaul10/24/04 18:45
mistake in dateAdd functiondrew09/13/04 23:00
RE: date diff & leap yearsAhad ali09/13/04 04:21
Find the start and end dates of a monthKumar05/05/04 08:24
RE: start date and end date of the weekKumar05/03/04 07:55
RE: start date and end date of the weekSunny03/07/04 01:42
SOLUTION: MYSQL -> PHPKarl Messner12/17/03 22:38
RE: Converting 2001-1-01 to 1-1-2001madjack08/08/03 19:35
Updated Quarter CodeIan05/19/03 22:50
RE: start date and end date of the weekselidori03/17/03 10:58
Get exact Time Difference betn two dates.Vijay02/17/03 05:16
RE: formatting date from a MySQL table PHP Bob12/03/02 00:40
RE: Expanded Date Diff - Years, Quarter, MontAllen11/25/02 10:51
RE: Trying to input a date from a form text inputmona vaishnav11/19/02 01:19
RE: using LIKE with wildcardsjoris11/17/02 21:22
RE: formatting date from a Ms-SQL table Fred Shen11/11/02 18:11
RE: strtotime doesn't workDan11/08/02 09:35
DONT NEED THIS, STRTOTIMEDan11/08/02 09:34
good work but.....Yassine11/01/02 04:30
Conversion to readable dateKevin10/31/02 09:10
Expanded Date Diff - Years, Quarter, Monthgeoff whittington10/17/02 12:42
upload fileliyla10/13/02 01:27
Date and timesathappan10/04/02 02:14
solution converting data from mysql to php Tom s09/24/02 09:00
start date and end date of the weekrakesh09/22/02 06:19
Epoch conversionTrueFaith09/19/02 03:22
Get all timstamps for a whole day?Arash09/02/02 17:18
How to get date from day of yearAmanda09/01/02 07:22
RE: formatting date from a Ms-SQL table Anitha08/28/02 05:44
Thanks a lot!Goblin08/27/02 08:26
RE: date comparisonAnurag08/27/02 06:33
Unix TimestampRichard Dorsett08/23/02 11:06
using LIKE with wildcardssmitam08/22/02 01:19
date comparisonsmita08/21/02 22:13
RE: Date HandlingKim Gan08/06/02 13:20
Display format date fetching date from oracleHaseeb ur Rehman07/23/02 01:51
server side link timer scriptrogie07/06/02 10:09
RE: datediff processnoshee07/02/02 06:31
DateDiff: Dates before 1970Seth06/19/02 16:16
Bug in DateAddAlberto Sylvester06/18/02 11:28
need to flag new listings... Fritz06/12/02 20:46
dynamic calendarrajkavi05/30/02 06:19
Cannot redeclare datediff() dsamsom05/29/02 13:35
Restriction of range dateDewi 05/23/02 05:35
Thanks You!Shelagh05/09/02 07:24
Thank You, Thank You, Thank You!!!Bob Langdon05/05/02 17:31
Format a MySQL timestamp ??Michael04/24/02 04:31
Many thanks for this articleDavid Court04/19/02 05:19
Great ArticleRyan Schwiebert04/16/02 11:27
date of next weekendHolger04/05/02 09:00
Adding these functions to the distributionMalcolm Davis04/04/02 08:38
RE: get Day from any given daterodney04/02/02 08:37
inserting count according to latest date.diksha04/01/02 01:16
get Day from any given datevipul patel03/21/02 11:51
Want to get date and time of subdirectoriesPakiya03/20/02 03:11
strtotime doesn't workAlex03/18/02 04:19
RE: Basic university projectmixi de France03/03/02 15:32
RE: friendly date scripotport-est02/23/02 15:06
friendly date scripote johnstone02/18/02 10:55
tree viewyana02/18/02 03:53
RE: Trying to input a date from a form text inputjdc4402/17/02 21:04
RE: Trying to input a date from a form text inputjdc4402/17/02 20:51
I am getting a wrong date when using date()Kishore Kadiyala02/16/02 00:49
HELP URGENT !!! Compare date and time Daven01/29/02 06:38
RE: I want time in my pagestewartg01/24/02 18:15
Dates and Times with PHP and MySQLstewartg01/24/02 03:09
RE: time zone of visitorstewartg01/24/02 03:05
RE: time zone of visitorstewartg01/24/02 02:40
RE: Trying to input a date from a form text inputstewartg01/24/02 02:25
RE: How to add datestewartg01/24/02 02:23
RE: Basic university projectstewartg01/24/02 02:16
RE: time spent by a visitor in a pagestewartg01/24/02 02:12
RE: epoch secondsstewartg01/24/02 02:01
simple date calculation (days difference)stewartg01/24/02 01:58
RE: I want time in my pageBob01/20/02 10:48
RE: I want time in my pageDeflatarat01/18/02 17:51
RE: no of days in monthDeflatarat01/18/02 17:46
RE: day addtion and substrationDeflatarat01/18/02 17:38
how to put a file into mysql databaseSorin 01/17/02 11:15
Translate date into other languages!Copycat12/30/01 12:41
Translate date into other languages!Copycat12/30/01 12:31
RE: day addtion and substrationJim Gantes12/16/01 05:15
day addtion and substrationpant12/08/01 06:25
formatting date in ODBCMike Page12/06/01 10:57
RE: Converting 2001-1-01 to 1-1-2001PackWolf12/04/01 20:18
RE: Converting 2001-1-01 to 1-1-2001Axel Brown11/30/01 01:06
Changing Dynamic Date formatofwofw11/29/01 10:23
RE: Converting 2001-1-01 to 1-1-2001hugo11/26/01 13:16
RE: Converting 2001-1-01 to 1-1-2001Protect11/24/01 22:31
RE: Converting 2001-1-01 to 1-1-2001beca4411/21/01 08:31
RE: Converting 2001-1-01 to 1-1-2001spiri11/13/01 02:25
RE: I want time in my pageAdam11/04/01 23:53
RE: Converting 2001-1-01 to 1-1-2001Greg11/04/01 01:19
Time zone manipulationDougal Campbell10/31/01 15:06
Converting 2001-1-01 to 1-1-2001madjack10/31/01 13:11
no of days in monthhimanshu10/30/01 05:01
RE: User Inserting DateAlex10/14/01 20:11
RE: Date differenceBjørn Rydén10/12/01 06:34
User Inserting DateAngela09/27/01 10:09
Date differenceRahil Mumtaz09/14/01 06:24
process form atheer09/05/01 19:50
I want time in my pageGreenpis08/28/01 19:18
RE: bcmath, why needed?bal08/18/01 09:30
I wanna just calculate the ageEdemilson Lima08/14/01 15:03
Date variableSteve08/14/01 08:47
how to dateDiff months?irf08/12/01 13:00
RE: formatting date from a MySQL table Emanweb08/12/01 03:21
date diff & leap yearsirf08/10/01 02:17
This function doesn't work.Michael Harris08/04/01 15:29
RE: time zone of visitorBenji07/31/01 08:23
RE: formatting date from a MySQL table loudi07/12/01 11:52
Trying to input a date from a form text inputDon07/11/01 14:10
How to add dateRamya07/11/01 00:51
Doesn't help meFrank Hilliard07/09/01 09:07
why "space" add befor month numbergosling07/02/01 21:40
RE: Apostrophe from a formDiablo722706/08/01 12:56
formatting date from a MySQL table php dummy06/05/01 16:52
RE: Apostrophe from a formLeed05/25/01 01:48
Apostrophe from a formRick Templin04/07/01 17:06
I NEED HELP !!!, I'M A MORON !HitthaGspt03/11/01 21:12
RE: bcmath, why needed?Nightslave03/01/01 01:09
RE: Basic university projectMatt M.02/15/01 08:41
Basic university projectMatthew Hanson02/08/01 08:24
thanksDoug Korthof02/04/01 18:12
RE: time zone of visitorJon Robins01/26/01 03:59
RE: time spent by a visitor in a pagekumar01/12/01 18:55
time zone of visitorJohn Derrickson01/03/01 14:34
How do I get time difference in VB or Java ScMahidhar12/14/00 08:25
RE: datediff processRafay Mohd12/11/00 10:34
RE: Remote machine dateRafay Mohd12/08/00 07:44
RE: datediff processJohannes11/29/00 09:48
Remote machine dateR.Sridhar11/09/00 12:39
bcmath, why needed?Paul Slootman10/05/00 09:48
epoch secondsWendy Roseberry09/21/00 18:44
For Nelson :: The first part of a solutionclee.pf.mc07/04/00 18:04
Time errorNelson Acero Fino07/04/00 09:04
time diference errorNelson Acero Fino07/01/00 19:23
datediff processNelson Acero Fino07/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.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.