Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Date manipulation in PHP
I was sitting writing a kind of helpdesk call management program in PHP and found that I needed to work out how much time had elapsed since somebody had last bothered to contact the client about their problem. In the past, when I used to use ASP, the solution was simple - ASP has a command called DateDiff which takes two dates and can tell you how many seconds has passed between them, how many days, months and so on. After scouring the PHP manual for a while I decided that PHP has no such beast. I then wondered how many other people there were out there like me, and thus this tutorial.
Ok, so what are we going to cover?
  1. Getting the current date and time - what options do we have ?
  2. Changing the way the date is displayed - date and time formatting
  3. Converting existing dates into Unix timestamps
  4. Back to the future - modifying the date
    1. Adding time to the current date
    2. Subtracting time from the current date
    3. Working out the time between two dates
  5. Creating a DateAdd function for PHP
  6. Creating a DateDiff function for PHP

[ Next Page ]


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.