Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,166 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,348 people online right now. Registration is fast and FREE... Join Now!




How to add time to date object

 
Reply to this topicStart new topic

How to add time to date object

kummu4help
29 Aug, 2008 - 10:36 PM
Post #1

D.I.C Head
Group Icon

Joined: 4 Aug, 2008
Posts: 184



Thanked: 2 times
Dream Kudos: 25
My Contributions
Hi
i'm using PHP 5.2.0
i've the following code

CODE

    $today=strtotime(date("h:i A"));

    
    $hour=date("h",$today);
    $min=date("i",$today);

    
    if($min<15)
    {
        $min=15;
    }
    elseif($min<30)
    {
        $min=30;        
    }
    elseif($min<45)
    {
        $min=45;
    }
    else
    {
        $min=0;
        $hour+=1;
    }
    
    date_add($today, new DateInterval("P$hourH$minM"));


here i want to add some amount of time to my $today object.
i.e
QUOTE
i want to increment hour value or minute value basing on the above if condition


in php manual i can see the date_add() function but when i'm trying it i'm getting following error
CODE

Fatal error: Call to undefined function date_add() in C:\Inetpub\wwwroot\filewar\next.php on line 32
PHP Fatal error: Call to undefined function date_add() in C:\Inetpub\wwwroot\filewar\next.php on line 32


can any one help me how to get rid off with this


User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: How To Add Time To Date Object
30 Aug, 2008 - 05:16 AM
Post #2

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
QUOTE
It is possible to use date() and mktime() together to find dates in the future or the past.

Example #3 date() and mktime() example
<?php
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
?>

Note: This can be more reliable than simply adding or subtracting the number of seconds in a day or month to a timestamp because of daylight saving time.


Directly from PHP's Date.

as for using date_add, it clearly says:
QUOTE
Warning

This function is EXPERIMENTAL. The behaviour of this function, its name, and surrounding documentation may change without notice in a future release of PHP. This function should be used at your own risk.


This post has been edited by JBrace1990: 30 Aug, 2008 - 05:18 AM
User is offlineProfile CardPM
+Quote Post

Ridikule
RE: How To Add Time To Date Object
30 Aug, 2008 - 06:52 AM
Post #3

D.I.C Head
**

Joined: 29 Sep, 2005
Posts: 58



Thanked: 4 times
My Contributions
Since $today is a timestamp, you can actually use the strtotime function to add amounts of any time unit.

$today = strtotime("+1 week 2 days 4 hours 2 seconds", $today);

You also could directly add time to timestamp using simple math operators, which might be slightly faster (but less readable).

$today += 3600; // adds an hour to $today


User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: How To Add Time To Date Object
30 Aug, 2008 - 07:05 AM
Post #4

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
while you can do it that way (adding x seconds), mktime is more reliable
User is offlineProfile CardPM
+Quote Post

kummu4help
RE: How To Add Time To Date Object
31 Aug, 2008 - 08:03 PM
Post #5

D.I.C Head
Group Icon

Joined: 4 Aug, 2008
Posts: 184



Thanked: 2 times
Dream Kudos: 25
My Contributions
Hi everybody,

thanks for ur responses i'll try to make sense from those

thanks for all
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:53AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month