user3462992
user3462992

Reputation: 175

Make a timer reset every 30 days?

I currently have code that changes the month number and MYSQL table every month automatically but the timer it displays still resets every 24 hours. I need to make it so the timer resets every month instead of every 24 hours. I am not thinking straight and need some help solving this.

Basically I need it so that $month_end_time counts down from 30 days, 0 hours, 0 minutes 0 seconds down to 0 days, 0 hours, 0 minutes 0 seconds and then resets back to the 30 days. Currently it counts down from 30 days to 29 days then resets as it is from a script that resets every 24 hours and I am porting it to monthly.

Credits to @ElmoVanKielmo for the original snippet.

Thanks in advance.

define("FIRST_DAY_STRING", "2014-4-6");
define("SHIFT_DAYS", 'P30D');
define("TIME_SUFFIX", " 0:00:00 GMT+11:00");

$today = new DateTime();
$first_day = new DateTime(FIRST_DAY_STRING);
$interval = $first_day->diff($today);
$days = $interval->format('%R%a days');
$end_date = $today->add(new DateInterval(SHIFT_DAYS));

$month_number = floor(intval($days) / 30 + 1);
$txid = "tx$month_number";
$month_end_time = $end_date->format('Y-n-j');
$month_end_time .= TIME_SUFFIX;

Upvotes: 0

Views: 1445

Answers (1)

Jared Farrish
Jared Farrish

Reputation: 49188

I suspect you're possibly overthinking this, since it includes "dates". When moving around months, it can be tricky, since (as Raptor notes), months have differing number of days between each other.

However, based on your comments, you're actually looking for the number of 30-day periods between one date and another. This can be accomplished with basic math and Unix timestamps:

$start = strtotime('2012-04-12 00:00:00 GMT');
$today = strtotime('00:00:00 GMT');
$days = ($today - $start) / 60 / 60 / 24;
$months = $days / 30;

echo "<pre>
Days: $days
Months: $months
";

This will give:

Days: 723
Months: 24.1

http://codepad.viper-7.com/KBVfqq

And if you're trying to figure out how many days:

$start = strtotime('2012-04-12 00:00:00 GMT');
$today = strtotime('00:00:00 GMT');
$days = ($today - $start) / 60 / 60 / 24;
$months = $days / 30;
$months_days = floor($months) . " months, " . ($days - (floor($months) * 30)) . " days";

echo "<pre>
Days: $days
Months: $months
Months and Days: $months_days
";

Giving:

Days: 723
Months: 24.1
Months and Days: 24 months, 3 days

http://codepad.viper-7.com/AdnFsu

Which means that between the start and today's date, there have been 24 full 30-day periods, and we are currently in the 25th period (ceil($months)). This seems sufficient for what you are after, although the specific use of the period value may require better explanation.

Upvotes: 1

Related Questions