Jump to content

PHP Date


deltajam_v

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

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.