By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Variable substitution to replace switch

P: n/a
http://www.phpbuilder.net/columns/ak...10.php3?page=5
>From this page I made this function to add time to a datetime
variable:

function addtime ($datetime, $add, $type)
{
// How to add time to $datetime (data type datetime) and return it as
datetime on the format 'Y-m-d H:i:s'
// example calls:
// $datetime = addtime ($datetime, 4, 'hours') // add 4 hours
// $datetime = addtime ($datetime, 2, 'months') // add 2 months
// $datetime = addtime ($datetime, 7, 'days') // add 1 week
// tested it with: die($now.'<br />'.addtime($now, 4, 'months'));
// anomaly: adding 4 months to 2007-05-31 09:50:52 gave 2007-10-01
09:50:52. I would have preferred 2007-09-31 09:50:52
$timestamp = strtotime($datetime);
$date_time_array = getdate($timestamp);
$hours = $date_time_array['hours'];
$minutes = $date_time_array['minutes'];
$seconds = $date_time_array['seconds'];
$months = $date_time_array['mon'];
$days = $date_time_array['mday'];
$years = $date_time_array['year'];

switch ($type)
{
case 'hours':
$hours = $add + $hours;
break;
case 'minutes':
$minutes = $add + $minutes;
break;
case 'seconds':
$seconds = $add + $seconds;
break;
case 'months':
$months = $add + $months;
break;
case 'days':
$days = $add + $days;
break;
case 'years':
$years = $add + $years;
break;
}
$timestamp = mktime($hours, $minutes, $seconds, $months, $days,
$years);
$newdatetime = date('Y-m-d H:i:s', $timestamp);
return ($newdatetime);
}

It works OK, but I would like to replace the switch statement with one
statement a la:

&$type = &$type + $add;

Can that be done in php?

Regards,

Jan Nordgreen

May 31 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
damezumari kirjoitti:
http://www.phpbuilder.net/columns/ak...10.php3?page=5
>>From this page I made this function to add time to a datetime
variable:

function addtime ($datetime, $add, $type)
{
// How to add time to $datetime (data type datetime) and return it as
datetime on the format 'Y-m-d H:i:s'
// example calls:
// $datetime = addtime ($datetime, 4, 'hours') // add 4 hours
// $datetime = addtime ($datetime, 2, 'months') // add 2 months
// $datetime = addtime ($datetime, 7, 'days') // add 1 week
// tested it with: die($now.'<br />'.addtime($now, 4, 'months'));
// anomaly: adding 4 months to 2007-05-31 09:50:52 gave 2007-10-01
09:50:52. I would have preferred 2007-09-31 09:50:52
$timestamp = strtotime($datetime);
$date_time_array = getdate($timestamp);
$hours = $date_time_array['hours'];
$minutes = $date_time_array['minutes'];
$seconds = $date_time_array['seconds'];
$months = $date_time_array['mon'];
$days = $date_time_array['mday'];
$years = $date_time_array['year'];

switch ($type)
{
case 'hours':
$hours = $add + $hours;
break;
case 'minutes':
$minutes = $add + $minutes;
break;
case 'seconds':
$seconds = $add + $seconds;
break;
case 'months':
$months = $add + $months;
break;
case 'days':
$days = $add + $days;
break;
case 'years':
$years = $add + $years;
break;
}
$timestamp = mktime($hours, $minutes, $seconds, $months, $days,
$years);
$newdatetime = date('Y-m-d H:i:s', $timestamp);
return ($newdatetime);
}

It works OK, but I would like to replace the switch statement with one
statement a la:

&$type = &$type + $add;

Can that be done in php?
I'm not sure if I understood, but let me introduce a cool function for
you: strtotime(), see http://php.net/strtotime

Basicly you can do this:
echo date('Y-m-d H:i:s', strtotime('2007-05-31 16:12:00 +2hours'));

Would this be useful to you?

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
May 31 '07 #2

P: n/a
At Thu, 31 May 2007 06:02:30 -0700, damezumari let h(is|er) monkeys type:
http://www.phpbuilder.net/columns/ak...10.php3?page=5
[snip]
>
switch ($type)
{
case 'hours':
$hours = $add + $hours;
break;
case 'minutes':
$minutes = $add + $minutes;
break;
case 'seconds':
$seconds = $add + $seconds;
break;
case 'months':
$months = $add + $months;
break;
case 'days':
$days = $add + $days;
break;
case 'years':
$years = $add + $years;
break;
}
$timestamp = mktime($hours, $minutes, $seconds, $months, $days,
$years);
$newdatetime = date('Y-m-d H:i:s', $timestamp);
return ($newdatetime);
}

It works OK, but I would like to replace the switch statement with one
statement a la:

&$type = &$type + $add;

Can that be done in php?
$$type += $add;

But, why are you reinventing the strtotime function?
$dayaftertomorrow = date('D M j G:i:s T Y',strtotime('+2 days'));

HTH,

Sh.

--
Schraalhans Keukenmeester - sc*********@the.spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples','oranges') < 0"

May 31 '07 #3

P: n/a
Thanks!

strtotime()= it is!

It would still be good to get my question answered:

"It works OK, but I would like to replace the switch statement with
one
statement a la:

&$type = &$type + $add;

Can that be done in php?"

Regards,

Jan Nordgreen

May 31 '07 #4

P: n/a
damezumari kirjoitti:
Thanks!

strtotime()= it is!

It would still be good to get my question answered:

"It works OK, but I would like to replace the switch statement with
one
statement a la:

&$type = &$type + $add;

Can that be done in php?"
If they are unix timestamps and seconds yes. If not, you need to convert
them into such first. This is what strtotime does, and then using date
again reverses it.

You can do this:
$time0 = time(); // get a unix timestamp
$interval = 60*60*24; // one day in seconds.
$time0 += $interval; // add an interval to timestamp
echo date('Y-m-d H:i:s', $time0); // tomorrow

But this is good just for timestamps. Just remeber, working with
timestamps is dreadful when it comes to daylight saving, leap years and
other temporal oddities.

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
May 31 '07 #5

P: n/a
Rami Elomaa kirjoitti:
damezumari kirjoitti:
>Thanks!

strtotime()= it is!

It would still be good to get my question answered:

"It works OK, but I would like to replace the switch statement with
one
statement a la:

&$type = &$type + $add;

Can that be done in php?"
Okay sorry, forget my reply, I've been reading your question wrong, I
didn't get what you actually were doing. Sorry, please ignore me. :)

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
May 31 '07 #6

P: n/a
At Thu, 31 May 2007 08:38:40 -0700, damezumari let h(is|er) monkeys type:
Thanks!

strtotime()= it is!
&$type = &$type + $add;

Can that be done in php?"
You missed that bit in my first reply I think...

$$type += $add;

Read about variable variables here:
http://www.php.net/manual/en/languag...s.variable.php

Rgds,
Sh.

--
Schraalhans Keukenmeester - sc*********@the.Spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples','oranges') < 0"

Jun 1 '07 #7

P: n/a
Yes, you are right. I did miss it.

Thanks for the solution and the link.

Regards,

Jan Nordgreen

Jun 2 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.