Jump to content

PHP & Time


Recommended Posts

Hi all, I am trying to figure out how to add 60 days to a mysql stored DATE, then subtract the current date from that date to see how many days there are... Its for a 60 day trial and when they signed up, it put the date in which they signed up in the database table...

 

Here's what I have, but it's not working... But I can't get it to add 60 days to a mysql stored DATE,... then of course I have to figure out how to do the subtraction...

$CustomerSaasDateStarted = $row['CustomerSaasDateStarted'];
$CustomerSaasDateStartedPlusSixtyDays = date("Y-m-d", strtotime($CustomerSaasDateStarted, "+60 days"));
// Test Output
echo 'Date Trial Started: '.date("M d, Y", strtotime($CustomerSaasDateStarted));
echo '<br />';
echo '60 days from Date Trial Started: '.date("M d, Y", strtotime($CustomerSaasDateStartedPlusSixtyDays));
echo '<br />';

Link to comment
Share on other sites

Ok, figured out how to add 60 days to a mysql date...

 

$CustomerSaasDateStartedPlusSixtyDays = date('M d, Y', strtotime($CustomerSaasDateStarted.' '.'+60 days'));

 

Now just need to figure out how to subtract the current date from that... will post code if I figure it out...

Link to comment
Share on other sites

Cannot seem to get a grip on this whole subtracting 2 dates things... I need to find out how many days are left from the subtraction...

 

This gives the wrong output, but like a month's worth...

 

$CustomerSaasDateStarted = $row['CustomerSaasDateStarted'];
$CustomerSaasDateStartedPlusSixtyDays = date('Y-m-d', strtotime($CustomerSaasDateStarted.' '.'+60 days'));
$TheCurrentDate = date('Y-m-d');
$TheCurrentDate = new DateTime($TheCurrentDate);
$CustomerSaasDateStartedPlusSixtyDays = new DateTime($CustomerSaasDateStartedPlusSixtyDays);
$DifferenceInterval = $TheCurrentDate->diff($CustomerSaasDateStartedPlusSixtyDays);
echo $DifferenceInterval->d.' Days Remaining on Trial<br /><br />

Link to comment
Share on other sites

You can do this all as part of your query -

SELECT DATEDIFF(DATE_ADD(CustomerSaasDateStarted, INTERVAL 60 DAY),CURDATE()) as days_remaining

 

A negative value would indicate the current date is past the end of the trial. A positive value would indicate the trial is still in effect.

Link to comment
Share on other sites

You can do this all as part of your query -

SELECT DATEDIFF(DATE_ADD(CustomerSaasDateStarted, INTERVAL 60 DAY),CURDATE()) as days_remaining

 

A negative value would indicate the current date is past the end of the trial. A positive value would indicate the trial is still in effect.

 

Thank  you, that worked perfectly, never thought to do it in the query...

Link to comment
Share on other sites

Actually, had to make one minor change to make the amount of days come out right, I didn't notice it at first until I started changing the date in which they signed up...

 

(60 - DATEDIFF(DATE_ADD(CustomerSaasDateStarted, INTERVAL 60 DAY),CURDATE())) as DaysRemaining

Link to comment
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.