Variable substitution to replace switch



http://www.phpbuilder.net/columns/akent20000610.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

.



Relevant Pages

  • Re: Variable substitution to replace switch
    ... function addtime ... // How to add time to $datetime (data type datetime) and return it as ... but I would like to replace the switch statement with one ...
    (comp.lang.php)
  • Re: Date formatting - Really newbie question
    ... you can use the following method to change the output of the datetime in this method. ... "Chuck G." wrote: ... imported from access and contains a column with data type datetime. ... No matter what I put in the style portion of the select statement it has no impact on my output format. ...
    (microsoft.public.sqlserver.mseq)
  • Re: Insert Date
    ... To be on the safe side, you should use parametrised sql insert command. ... SqlCommand cmd = new SqlCommand("INSERT INTO Table (..., datecolumn) VALUES ... You should convert your date to DateTime type: ... > I have a column with data type datetime. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Data Type conversion from int to datetime
    ... This conversion assumes the integer contains the number of days since ... you'll need to first convert it to character and then to datetime. ... CAST queries using various intermediate datatypes to get the conversion, ... Arithmetic overflow error converting expression to data type datetime. ...
    (microsoft.public.sqlserver.programming)
  • Re: Please advise on approach
    ... has direct bearing on the issues and the context is difficult to communicate ... > of the term timestamp - it does not refer to the sql data type in this ... rather to the datetime at which the source produced the data - ... I'm only interested in it differentiating between records ...
    (microsoft.public.sqlserver.server)