Jump to content

[SOLVED] php dateAdd function or similar?


Recommended Posts

hey all,

 

 

does it exist, various functions avaliable via google but not what im looking for

 

i need to add seconds to a timestamp formatted as "01/01/1990 00:00:00"

 

for example

 

01/01/1990 00:00:00 + 578530800 seconds = 06/05/2008 00:00:00

 

ive seen this method used in VB/ASP but not in php... sorry if this is breif...

 

iM

Link to comment
https://forums.phpfreaks.com/topic/104342-solved-php-dateadd-function-or-similar/
Share on other sites

awesome you put me on the right track there mate.

 

 function DateAdd($interval, $number, $date) {
    $date_time_array = getdate($date);
    $hours = $date_time_array['hours'];
    $minutes = $date_time_array['minutes'];
    $seconds = $date_time_array['seconds'];
    $month = $date_time_array['mon'];
    $day = $date_time_array['mday'];
    $year = $date_time_array['year'];
    switch ($interval) {
    
        case 'y':   // add year
            $year+=$number;
            break;

        case 'm':    // add month
            $month+=$number;
            break;

        case 'd':    // add days
            $day+=$number;
            break;

        case 'w':    // add week
            $day+=($number*7);
            break;

        case 'h':    // add hour
            $hours+=$number;
            break;

        case 'n':    // add minutes
            $minutes+=$number;
            break;

        case 's':    // add seconds
            $seconds+=$number; 
            break;            

    }
       $timestamp= mktime($hours,$minutes,$seconds,$month,$day,$year);
    return $timestamp;
}
$temptime = time();
echo strftime('%Hh%M %A %d %b',$temptime);

$temptime = DateAdd('s',578921162,'01/01/1990 00:00:00');
echo '<p>';
#echo strftime('%Hh%M %A %d %b',$temptime);
#echo strftime('%d/%m/%Y',$temptime);
$temptime = DateAdd('y', 20, $temptime);
echo strftime('%Hh%M %A %d %b',$temptime);

 

found this awesome function thats been put together by someone.

 

although i had to use it twice...

 

the initial result was 20 years short of what it needs to be...thus just adding 20 years :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.