Jump to content

Recommended Posts

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

[code]
<?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";
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/7722-php-date/#findComment-28181
Share on other sites

Just use the [a href=\"http://www.php.net/strtotime\" target=\"_blank\"]strtotime()[/a] function to add the months:
[code]<?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;
?>
[/code]

Ken
Link to comment
https://forums.phpfreaks.com/topic/7722-php-date/#findComment-28187
Share on other sites

  • 7 months later...
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
Link to comment
https://forums.phpfreaks.com/topic/7722-php-date/#findComment-127963
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.