473,396 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Variable substitution to replace switch

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
7 1904
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
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
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
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
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
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
Yes, you are right. I did miss it.

Thanks for the solution and the link.

Regards,

Jan Nordgreen

Jun 2 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: bofh | last post by:
I'm working on a project where I need to store a CGI query in a database field. The query contains variables which will be substitued at runtime (e.g., today's date, key to select upon, etc), and...
1
by: Andy | last post by:
We are running a DTS package with the dtsrun utility and would like to pass a variable through it. Inside our package we have a VB script that references a table that contains the information...
1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
22
by: bmgz | last post by:
sorry for the stupid question, bu6 I haven't been able to find the answer anywhere.. consider this useless function which assigns an object to a var.. function(myParam){ var select1 =...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
1
by: Florian Schulze | last post by:
See the following results: Python 2.3.5 (#62, Feb 8 2005, 16:23:02) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import re >>> s = "1" >>>...
2
by: Sike | last post by:
Hi everyone, I've been browsing this and a few other related newsgroups trying to get my head around this problem, and so far all the trails seem to go cold, without an acceptable solution being...
6
by: Generic Usenet Account | last post by:
I was extremely surprised to learn that the extremely rich C++ string API does not have even a single menthod devoted to string substitution i.e. given a string, replace all instances of pattern-1...
2
by: andreb7 | last post by:
Hi everyone I'm frsutrated by this problem and would really appreciate a hand. I may not be doing this the best way, so any suggestions are more than welcome...I want to replace certain sections of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.