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;
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.
Related
i try to make time difference with carbon
$dt = Carbon::parse('2018-07-15 00:00:00');
$now = Carbon::now('Asia/Dubai'); //set current time
$seconds = $now->diffInSeconds( $dt ); //difference turn into second
$days = $dt->diffInDays($dt->copy()->addSeconds($seconds));
$hours = $dt->diffInHours($dt->copy()->addSeconds($seconds)->subDays($days));
$minutes = $dt->diffInMinutes($dt->copy()->addSeconds($seconds)->subHours($hours));
$days result are 12 (its right).
$hours result are 8 (seems not right).
$minutes result are 17299 (clearly wrong).
how to get the result for example 12 day 5 hours 45 minutes
Actually functions like diffInSeconds give total difference in seconds that's why the number is so large,to get the minutes for the time difference right you can use -:
$minutes = ($now->minute - $dt->minute);
This question already has answers here:
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 7 years ago.
I have two date times of the form
Start Date: 2015-11-15 11:40:44pm
End Date: 2015-11-22 10:50:88am
Now I need to find the difference between these two in the following form:
0 years, 0 months, 7 days, 22 hours, 44 mints, 35 sec
How can I do this in PHP?
I already try:
$strStart = date('Y-m-d h:i:s', time() - 3600);
$strEnd = '2015-11-22 02:45:25';
$dteStart = new DateTime($strStart);
$dteEnd = new DateTime($strEnd);
$dteDiff = $dteStart->diff($dteEnd);
echo $dteDiff->format("%H:%I:%S");
Output:22:53:58
Output not shown perfectly.
Now I need to find the difference between these two in the following form:
0 years, 0 months, 7 days, 22 hours, 44 mints, 35 sec
So that’s your main problem here, getting this exact output structure?
Well then you simply have to format the DateInterval differently:
echo $dteDiff->format("%y years, %m months, %d days, %h hours, %i mints, %s sec");
$startDate = "2015-11-15 11:40:44pm";
$endDate = "2015-11-22 10:50:48am"; // You had 50:88 here? That's not an existing time
$startEpoch = strtotime($startDate);
$endEpoch = strtotime($endDate);
$difference = $endEpoch - $startEpoch;
The script above converts the start and end date to epoch time (seconds since January 1 1970 00:00:00 GMT). Then it does the maths and gets the difference between them.
Since years and months aren't a static value, I haven't added them in the script below
$minute = 60; // A minute in seconds
$hour = $minute * 60; // An hour in seconds
$day = $hour * 24; // A day in seconds
$daycount = 0; // Counts the days
$hourcount = 0; // Counts the hours
$minutecount = 0; // Counts the minutes
while ($difference > $day) { // While the difference is still bigger than a day
$difference -= $day; // Takes 1 day from the difference
$daycount += 1; // Add 1 to days
}
// Now it continues with what's left
while ($difference > $hour) { // While the difference is still bigger than an hour
$difference -= $hour; // Takes 1 hour from the difference
$hourcount += 1; // Add 1 to hours
}
// Now it continues with what's left
while ($difference > $minute) { // While the difference is still bigger than a minute
$difference -= $minute; // Takes 1 minute from the difference
$minutecount += 1; // Add 1 to minutes
}
// What remains are the seconds
echo $daycount . " days ";
echo $hourcount . " hours ";
echo $minutecount . " minutes ";
echo $difference . " seconds ";
I have a dynamic date, now what i want is that finding the date after exact one week, i have achieved that with the code below, but now i want that now many days are left for that week after date to come. i have got some sort of time stamp, but i don't know how to convert it to DAYS LEFT.
$weekDate = date( "d/m/Y", strtotime("19-05-2014") + 86400 * 7 );
echo $weekDate;// THATS PERFECT
////////////////////////////////////////////////////////////////
$future = strtotime( $weekDate ); //Future date.
$datediff = time() - $future;
$days = floor( ( ( $datediff / 24 ) / 60 ) / 60 ); //this is not perfect, returns some
sort of timestamp
I have tried other methods which are fine, but if week completes on 26, and today is 25th it gives me 0 days left, but it should say 1 day left. please help me.
In your $date_diff now is less than the future date thats why its zero. Inside strtotime() function, you can directly put a relative date inside. In this case, for one week you can use +1 week or +7 days. Consider this example:
$next_week = date('d/m/Y', strtotime('19-05-2014 +1 week')); // 26/05/2014
$next_week = strtotime('19-05-2014 +7 days');
$difference = $next_week - time(); // next weeks date minus todays date
$difference = date('j', $difference);
echo $difference . (($difference > 1) ? ' days ' : ' day ') . ' left';
// should output: 1 day left
Alright. I did something. Here's the code
$startDate = strtotime("19-05-2014");
$endDate = $startDate + 604800;
$diff = ($endDate - time()) / 60 / 60 / 24;
if ($diff < 1 && $diff > 0) {
$days = 1;
} else {
$days = floor($diff);
}
echo $days;
The problem you have with getting "1 day" if the date is tomorrow is the floor method. strtotime() gives you the time at 0 a.m. if you don't set it by your own. Because of that the difference between now and tomorrow is less than 1 which is 0 if you floor that. I created an if-clause for that.
But that will give you "1 day" for today and "1 day" for yesterday (last 2 days before the final date). If you want that better, you have to specify time in your initial date (19-05-2014).
Use DateTime for date and time calculations.
$weekDate = new \DateTime('+ 1 week');
$future = new \DateTime('+ 3 days');
$daysLeft = $weekDate->diff($future)->days;
echo $daysLeft; //4
See it working.
Reference http://php.net/datetime
I know there might be different ways using timestamps and stuff but I'm having trouble converting number of hours into something that human would understand. I do not have power to change anything in the database.
There is a column that holds number of hours, so it can be something like 134.37 hours. Now I can not display that and tell user that something will happen in 134.37 hours I need to convert it into months, days, hours, minutes, seconds.
For example:
Given Hours: 23.33
Desired Result: 0 Months, 0 Days, 23 Hours, 19 Minutes, 48 seconds (dont care about seconds)
Now I need months and days since number of hours might be large. The code I started with does give me number of hours, minutes and seconds but i cant get days and months.
$months = $days = $hour = $min = $sec = 0;
$decimalHours = 23.33;
//convert to hours
$hour = (int)$decimalHours;
$decimalHours -= $hour;
//convert to minutes and subtract minutes
$decimalHours *= 60;
$min = (int)$decimalHours;
$decimalHours -= $min;
$decimalHours = number_format($decimalHours, 10);
//convert to seconds
$decimalHours *= 60;
$sec = (int)$decimalHours;
echo $hour . ' hours, ' . $min . ' minutes, ' . $sec . ' seconds';
Please help if you know a function that does it or an easier way.
You can achieve this with DateTime extension:
$hours = 23.33;
$zero = new DateTime('#0');
$offset = new DateTime('#' . $hours * 3600);
$diff = $zero->diff($offset);
echo $diff->format('%m Months, %d Days, %h Hours, %i Minutes');
demo
Code new DateTime('#0'); creates DateTime object with timestamp 0, which is January 1 1970 00:00:00 GMT. Timestamp 0 is zero number of seconds since the Unix Epoch.In this example it basically doesn't matter how you create DateTime object, I just wanted it to be in UTC offset and to ignore DST. You can also create DateTime object like new DateTime('UTC'); (which is current datetime in UTC timezone) or something familar.
Edit:
I guess I can ignore months and display days + hours + minutes is better than just hours
In that case just use echo $diff->format('%a Days, %h Hours, %i Minutes');. See the difference where I replaced format of days from %d to %a. Read the DateInterval::format() what this characters mean. You can also access parameters directly on DateInterval objects as echo $diff->days; echo $diff->h; // etc. (use print_r($diff); to see those parameters).
How long is a month? 30 days? 31 days? 30.5 days? 365.24 / 12 ?
Skipping that, you can do:
$hours = 23.33;
$days = floor($hours / 24);
$remaining_hours = $hours - $days * 24;
$hours = floor($remaining_hours);
$minutes = round(($remaining_hours - $hours) * 60);
echo $days . " days " . $hours . " hours " . $minutes . " minutes";
// 0 days 23 hours 20 minutes
First off the hours thing is bonkers. I'm assuming they are always adjusted to be current (ie 10 hours to something happening... 9 hours.. 8 hours)
But have you tried a simple php strtotime() approach? Format your output to a date/time/countdown using timestamp?
$dateFromToday = strtotime('+23.33 hours'); // get unixtimestamp from today + hours
echo date('l jS \of F Y h:i:s A', $dateFromToday); // format my date output
Maybe I am oversimplifying it tho.
If you populate the $datetime variable (sorry for unconventional variable names), the following code applies.
This:
$original = 23.33;
$hours = floor($original);
$minutes = floor(60 * ($original - $hours));
echo sprintf('Total: %s hours, %s minutes', $hours, $minutes);
echo '<br />';
$datetime = new \DateTime('-2 hours 15 minutes');
$destined = new \DateTime(sprintf('+ %s hours %s minutes', $hours, $minutes));
echo sprintf('Scheduled Time: %s', $destined->format('Y-m-d sH:i:s'));
echo '<br />';
$interval = $destined->diff($datetime);
echo sprintf('Time Remaining: %s months, %s days, %s hours, %s minutes',
$interval->m, $interval->d, $interval->h, $interval->i);
Outputs:
Total: 23 hours, 19 minutes
Scheduled Time: 2014-01-04 1915:15:19
Time Remaining: 0 months, 1 days, 1 hours, 4 minutes
I am completely stuck here. I am trying to get how many days hours and minutes to echo from a calculation from the current time to 7 days from now at 6pm. I look at the amount of seconds produced from my $difference variable and when I do the math to convert it to days hours and minutes it is correct but for some reason when I call the specific days, hours, and minutes in my output statement it is incorrect. What am I doing wrong. Here is the code.
<?php
date_default_timezone_set('America/New_York');
$nextWeek = strtotime('+7 days');
$m = date('n', $nextWeek);
$d = date('j', $nextWeek);
$y = date('Y', $nextWeek);
$difference = mktime(18,0,0,$m,$d,$y) - time();
echo '<p>Current date and time is' .date(' l F d, Y '). 'at '.date('g:i a').' You have an appointment in a week on '.date(' n/j/Y ', $nextWeek).' at 6pm. There are ' .date(' j ', $difference).' days, ' .date(' g ', $difference).' hours, and ' .date(' i ', $difference).' minutes until your appointment.</p>';
echo mktime(18,0,0,$m,$d,$y),"\n";
echo $difference;
?>
The problem is that you're using PHP's date() function on a number that doesn't represent a date. Your variable $difference represents the difference between the two dates, in seconds. To get the right output, you should write your own function to convert these seconds to number of days, hours, minutes, etc.
It might look something like this:
function getTimeText($seconds)
{
$return = array();
$return["days"] = floor($seconds/86400); // 86400 seconds in a day
$seconds -= ($return["days"]*86400);
$return["hours"] = floor($seconds/3600); // 3600 seconds in an hour
$seconds -= ($return["hours"]*3600);
$return["minutes"] = floor($seconds/60); // 60 seconds in a minute
return $return;
}
Try checking out this. The example part way down the page shows you how to find the difference between two dates in days. You should be able to use it to return the difference between the current time and the time in 7 days at 18:00 by changing the format.