subtracting hours from date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

subtracting hours from date

Post by GeXus »

Code: Select all

$offset = "50";
$date = date("M-d-Y", mktime(date('H')-$offset, date('i'), date('s'), date('m'),  date('d'), date('Y')));
This is what I have to subtract hours from the current day... the issue is that this only works with subtracting, and I want it to add as well... So ideally, I would have something like this..

Code: Select all

$offset = "+50";
$date = date("M-d-Y", mktime(date('H')$offset, date('i'), date('s'), date('m'),  date('d'), date('Y')));
Of course, that does not work (unexpected variable).... Any ideas how to do this?

thanks!
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Why is the `+` in the Variable?

Code: Select all

$offset = 50;
$date = date("M-d-Y", mktime(date('H')+$offset, date('i'), date('s'), date('m'),  date('d'), date('Y'))); 
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

I guess I can just create two functions one for add one for subject.... I was just thinking there might be a more efficient way that I could add and subtract with one instance
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Why not just have a condition statement, or do the addition/subtraction before hand?

Code: Select all

$Offset = date('H')+50;
$Time = offsetHrs($Offset);
// 
$Offset = date('H')-50;
$Time = offsetHrs($Offset);
rodrigoreis
Forum Newbie
Posts: 1
Joined: Fri Oct 26, 2007 7:48 pm

Post by rodrigoreis »

Hi Everyone!

I´m working on a project, that has something to do with this subject.. I builded a flash clock, that loads a bunch os stuff depending on the server time, using a PHP file.

The script is working fine, I just need some help to adjust to my local time (+3 hours), since I know how to write anything in PHP...

If you could help me out here, i´d really appreciate!!!!

Here´s the PHP code:

Code: Select all

<?
print ("serverHour=".date("G:i:s A")."&weekDay=".date("l")."&date=".date("d.m.Y")."&");
?>
This returns 3 dinamic text field in Flash... I just need to add 3 hours..

Thanks!

Rodrigo
mrkite
Forum Contributor
Posts: 104
Joined: Tue Sep 11, 2007 4:19 am

Post by mrkite »

Zoxive wrote:Why is the `+` in the Variable?

Code: Select all

$offset = 50;
$date = date("M-d-Y", mktime(date('H')+$offset, date('i'), date('s'), date('m'),  date('d'), date('Y'))); 
Just in case Zoxive didn't make it clear, offset can be negative and it'll subtract.

Code: Select all

$offset = -50;
$date = date("M-d-Y",mktime(date('H')+$offset, date('i'), date('s'), date('m'), date('d'), date('Y')));
Post Reply