php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #44163 Addition of more useful date time functions
Submitted: 2008-02-19 05:05 UTC Modified: 2008-07-08 18:05 UTC
From: cool_lim_lp at yahoo dot com dot sg Assigned: derick (profile)
Status: Closed Package: Feature/Change Request
PHP Version: 5.2.5 OS: any
Private report: No CVE-ID: None
 [2008-02-19 05:05 UTC] cool_lim_lp at yahoo dot com dot sg
Description:
------------
Is it possible to add more useful functions like date substraction and date addition and the ability to get individual date parts?

For example, to get a date part, we have to do something like

$datetime  = new  DateTime ('12 jan 2008');

$day = (int)$datetime->format('d');

We have to convert an internally stored int to a string and then back to an int again, duh.
Why not simply introduce a 'getDay' function?

$day = $datetime->getDay();
$year = $datetime->getYear();


Subtraction of two dates:
First, we have to convert the datetime object to a UNIX timestamp, which has a very limited range and then perform some maths on this timestamp itself. It is very tedious and troublesome.

for example to calculate the age of  a person:
$now = new DateTime ('now');
$dob = new DateTime ('19 jan 1955');

you have to first convert to a UNIX timestamp, but the dob is out of the range allowed. Or you could use date_parse to get some info and then perform some lengthy maths to calculate the age.


Sometimes we also need to know the amount of time that has elapsed between two dates in terms of hours, minutes, seconds, years, or months  etc. But currently, there is no easy way to calculate that using PHP 's built-in functions.


 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-19 05:10 UTC] rasmus@php.net
That's already there.  Please see the date_modify() function in the manual.  Also see date_parse().


 [2008-02-19 05:26 UTC] cool_lim_lp at yahoo dot com dot sg
ok, tell me how do you calculate the no. of days difference between two given dates.
 [2008-02-19 05:46 UTC] cool_lim_lp at yahoo dot com dot sg
look at the user notes at :

http://sg2.php.net/manual/en/function.date.php

see how many people have to write their own functions to do even simple date calculations.
 [2008-02-19 05:59 UTC] rasmus@php.net
Right, date_diff would be useful, but date addition and the ability to get individual date parts are there already in date_modify() and date_parse().
 [2008-02-19 08:05 UTC] derick@php.net
Rasmus, most of the functions in the date() comments actually don't work, so we can use something like this. I am hoping to manage to get it into PHP 5.3.
 [2008-02-19 08:48 UTC] rasmus@php.net
I didn't refer to the date() comments.  The bug reporter did.
 [2008-07-08 18:05 UTC] derick@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

date differences and intervals are part of PHP 5.3.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 07 07:01:32 2024 UTC