Jump to content

PHP & Time


Hobbyist_PHPer

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

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.