Jump to content


Photo

PHP Date


  • Please log in to reply
6 replies to this topic

#1 deltajam_v

deltajam_v
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 18 April 2006 - 02:10 PM

Hi, I'm pulling a datetime field called 'expires' from the database. I also have a field in the db called 'months' that I would like to add to the expires field.

So if:

$expires = 2006-04-17 09:54:23;
$months = 6;

I want to add the months to the expires variable. How would I do that?

#2 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 18 April 2006 - 02:42 PM

I hope I understand you correctly. If so, this will do what you want.

<?php

    // Your original data
    $expires = "2006-04-17 09:54:23";
    $months = "6";

    // Break the $expires variable into component parts
    $year = substr($expires, 0, 4);
    $month = substr($expires, 5, 2);
    $day = substr($expires, 8, 2);
    $hour = substr($expires, 11, 2);
    $minute = substr($expires, 14, 2);
    $second = substr($expires, 17, 2);

    // Add $months onto $month
    $new_month = $months+$month;

    // If $new_month is only 1 digit, add a preceeding zero
    $strlen = strlen($new_month);
    if($strlen==1) {$new_month = "0$new_month";}

    // Set the $expires variables with the new value
    $expires = "$year-$new_month-$day $hour:$minute:$second";

    // Just so you can see that it works... echo the result.
    echo "$expires";
?>

wisewood: proven fact, I am both wise, and wooden.

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 18 April 2006 - 02:53 PM

Just use the [a href=\"http://www.php.net/strtotime\" target=\"_blank\"]strtotime()[/a] function to add the months:
<?php
    // Your original data
    $expires = "2006-04-17 09:54:23";
    $months = "6";
    $expiration_date = date('Y-m-d H:i:s',strtotime($expires . ' + ' . $months . ' + months'));
    echo $expires . ' + ' . $months . ' months = ' . $expiration_date;
?>

Ken

#4 deltajam_v

deltajam_v
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 18 April 2006 - 03:13 PM

Thank you very much kenrbnsn. Not to discredit wisewood though ... both work perfectly. Thank you!

#5 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 18 April 2006 - 03:36 PM

Don't worry... ken knows more clever stuff than i do
wisewood: proven fact, I am both wise, and wooden.

#6 deltajam_v

deltajam_v
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 21 November 2006 - 03:52 PM

When I use kenrbnsn's code, the output is pretty wacky:

2006-04-17 09:54:23 + 6 months = 1969-12-31 19:00:00


any suggestions?

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 21 November 2006 - 04:01 PM

That will happen when the strtotime() functon does not recognize the input as a valid date/time string.  I just tested the code again and it works fine. Please post the code you used to get the invalid result. Also what changed since April when you said it worked fine.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users