Adding days to a date value and inserting the new value into a date field

18 posts by 3 authors in: Forums > CMS Builder
Last Post: February 11, 2015   (RSS)

By claire - January 20, 2015

Yeah, I think so. If you don't have to calculate it on the fly, then it does get easier.

Hardcoded checks are not ideal, but they'll be fine as long as you've only got a couple to deal with.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By claire - January 20, 2015

I'd probably need a while to code up something that works, but the basic jist of it is that you'd get the current UNIX timestamp from time(), get your cutoff date and turn it into another timestamp using strtotime(), then check if the current timestamp is before or after it. Then you'd manually set the expiresDate to a specific date and time instead of trying to add on a year or more.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Hi Claire,

Sorry to come back to this, but I’m still trying to add one year (or multiple years) to an expiresDate and can’t lick it.

Here’s where I’m at so far...

I tried the adding seconds using strtotime as you suggested, and I come up with a leap year issue because the new date is one day short for leap years. (Oct 1, 2015 becomes Sept 30, 2016 and  Sept 30, 2019 becomes Sept 29, 2020, etc.)

<?php $current_expiration = $CURRENT_USER['expiresDate'] ?>
  <?php $expires_date_plus_1_year = date('Y-m-d H:i:s', strtotime($current_expiration) + 60*60*24*365); ?>
<?php
  mysqlStrictMode(false);
 mysql_query("UPDATE `{$TABLE_PREFIX}accounts`    
    SET
    expiresDate       = '".mysql_escape($expires_date_plus_1_year)."'
   WHERE num = '".mysql_escape( $CURRENT_USER['num'] )."'")  
       or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n");  
        $userNum = mysql_insert_id();  
    // redirect after a successful update  
    header("Location:  $masterurl/renewal-confirmation.php");
 ?>


Then I tried using add_date and it throws the following mySQL error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DATE_ADD('2020-09-29 00:00:00', INTERVAL 5 YEAR); WHERE num = '4'' at line 3

(the date 2020-09-29 is correct, as is the user number '4")

 <?php $current_expiration = $CURRENT_USER['expiresDate'] ?>
<?php
  mysqlStrictMode(false);
 mysql_query("UPDATE `{$TABLE_PREFIX}accounts`    
    SET
     DATE_ADD('$current_expiration', INTERVAL 5 YEAR);
   WHERE num = '".mysql_escape( $CURRENT_USER['num'] )."'")  
       or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n");  
        $userNum = mysql_insert_id();  
    // redirect after a successful update  
    header("Location:  $masterurl/renewal-confirmation.php");
 ?>


Any thoughts?

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By claire - February 11, 2015

Hey Jerry

I think you're overthinking this. You already know that the renewal date will be September 30th or October 1st, yeah? It's only the year that changes. So your code doesn't need to add on a certain number of days - you effectively only need to work out what year it should be in.

I think some PHP logic should sort this out - as in, looking at the current time, working out whether it's before or after January 1st, and then setting the year manually instead of using the date function.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Sounds like a more elegant solution, Claire,

I know I'm pushing the envelope, but could you offer any code snippet that I could use to do this? 

I'm feeling very programming challenged by this for some reason. (I think I always feel challenged about working with dates.)

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By claire - February 11, 2015

Hi Jerry

I've got nothing written up, sorry. This would be considered a custom addition, though it's likely a pretty quick one. Can you email Ross at consulting@interactivetools.com with a link to this thread? 

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Will do.

And thanks for all your help.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php