473,804 Members | 3,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($date time);
$date_time_arra y = getdate($timest amp);
$hours = $date_time_arra y['hours'];
$minutes = $date_time_arra y['minutes'];
$seconds = $date_time_arra y['seconds'];
$months = $date_time_arra y['mon'];
$days = $date_time_arra y['mday'];
$years = $date_time_arra y['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 1930
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($date time);
$date_time_arra y = getdate($timest amp);
$hours = $date_time_arra y['hours'];
$minutes = $date_time_arra y['minutes'];
$seconds = $date_time_arra y['seconds'];
$months = $date_time_arra y['mon'];
$days = $date_time_arra y['mday'];
$years = $date_time_arra y['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*********@gma il.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?
$dayaftertomorr ow = date('D M j G:i:s T Y',strtotime('+ 2 days'));

HTH,

Sh.

--
Schraalhans Keukenmeester - sc*********@the .spamtrapexampl e.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*********@gma il.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*********@gma il.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 .Spamtrapexampl e.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
2213
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 may be pointed to different URLs depending upon the table key. The variable substitution works fine when hardcoding into the script, e.g.: $var1='data1';
1
5831
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 about all of our jobs and we would like to pass a variable to tell it which jobs to run. The reason we cannot just add the variable to the VB script, i.e. in a where clause, is because we would like more then 1 bat file to run this package. We want...
1
4562
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 name="Tim">ZXCVBNM</Variable> <Function id="1"> <Parameter type="String">Bob</Parameter> <Parameter type="String">Steve</Parameter>
22
1616
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 = document.myForm.mySelectName; } I simply want this line to subsitute "mySelectName" with a var string
166
8694
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
1083
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" >>> re.sub('1','\\n',s) '\n' >>> '\\n'
2
2979
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 reached. I'm posting here because there seems to be a few MVP's knocking around, and if they dont know, then it's a safe bet nobody does. I'm beginning to think that what I want to do is simply not possible - but i'll put it out there once...
6
6337
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 in the string with pattern-2. There are API methods for finding and replacing, but none on pattern substitution. Although I have developed an implementation for this (posted to the comp.sources.d newsgroup), does anyone have the background why...
2
2442
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 a string with other sections (substring replacements). For example, I would like to go from something like this : <TAG val="XXX">a hundred</TAG><TAG val="XXX">a thousand</TAG> to something like this : <TAG val="900">nine hundred</TAG><TAG...
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10326
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.