How to get days and months number from a given days - php

I'm going to write a function to print a number of days left between two dates. I would like it to tell the months and days left. For example:
45 days = 1month, 15 days
65 days = 2months, 5 days
10 days = 10 days
So I tried:
<?
$days=50;
if($days>"31"){
$time=$days/30;
}
echo $time;//1.67 month
?>
According to the code above. I expect the result to be like:
1 month, 20 days
Could you guys please suggest.

Try:
$days = 50;
if ($days > 31){
$month = floor($days/30); // return lowest whole integer
$days = $days % 30; // calculate left days
}
echo $month . " => " . $days; // output `1 => 20`

Get the month number for both months and subtract. Add in calculation for year change
Get day of months for both dates. If date2 > date1, subtract and you have the number of days, else remove 1 from month count and sumteact date

Related

dividing a number by the day of month only returning single digit number

I am trying to calculate what pace our sales team in on for the month, but when I divide our numbers so far (17,305) by the day of the month (08), I am getting the wrong number (2.125).
I've tried converting the date from a string to a number, but I everything I read says php should know how to handle the numbers when it's a string or number.
$dateday = date('d');
$numberofdays = date('t');
echo $ztmmoney.' total for month<br>';
echo $dateday.' day of the month<br>';
$mavg = $ztmmoney/$dateday;
echo $mavg.' daily average<br>';
echo $numberofdays.' days in month<br>';
$pace = $mavg * $numberofdays;
echo 'on pace for: '.$pace;
I should see 17,305/8 = 2,163.125
but this is my output:
17,305 total for month
08 day of the month
2.125 daily average
31 days in month
on pace for: 65.875
You can use this to get rid of the wrong calculation
$ztmmoney = '17,305';
$ztmmoney = intval(preg_replace('/[^\d.]/', '', $ztmmoney));

Counting number of days between 2 dates when exceed in 1 hour add another 1 day

I'm having problem when getting the exact number of days. Given I have date/time which consider hours in counting number of days below the code give me zero days
$fisrstDate = new DateTime("2018-03-07 04:46:00");
$secondDate = new DateTime("2018-03-07 11:10:00");
$days=$fisrstDate->diff($secondDate)->days;
another example is this it should give me 2 days but shows only 1 days my idea is when 24 hours exceed I want to add another 1 days so that it would give me an output of 2 days
$fisrstDate = new DateTime("2018-03-07 04:46:00");
$secondDate = new DateTime("2018-03-08 05:00:00");
$days=$fisrstDate->diff($secondDate)->days;
You can use strtotime to get the exact seconds between two time stamps and then convert it to days followed by ceil to make it work. Eg:
$fisrstDate = strtotime("2018-03-07 04:46:00");
$secondDate = strtotime("2018-03-07 11:10:00");
$days = abs(ceil((abs($fisrstDate - $secondDate)/ (60 * 60 * 24)) - (1 / 24)));
echo $days;
Isn't it just date2 - date1 + 1?

PHP - Intelligent Adding Months to Date

I've just discovered a problem with the usually used method to sum months to a PHP Date. If you search on google or this forum, you usually find somethings like these:
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
or
$months = DateInterval::createFromDateString('1 month');
$dateInDateTime->add($months);
Both approach are not correct, in my opinion.
For example in my code I have to increment 3 times the month of a starting date beginning with last day of April and return the last day of that months.
So my code generates this results:
2017-04-30
2017-05-31
2017-07-31
The second time the script add +1 month to date, goes from 2017-05-31 to 2017-07-01 because 31-05 + 30 days is over the last day of JUNE.
What Im expecting is 06-30 because you are summing MONTHS not DAYS and if you have an overflow, the code has to correct it, not me.
This is a common error that explode when you manage February or December (due to change of year).
Im expecting a script that increment month. So if I have 2017-03-23 and sum +1 month, I get 2017-04-23 and if I sum +1 month to 2017-03-31 I got 2017-04-30.
So. Pay attention when using this functions.
I think you are trying something dangerous.
What s going on for february? if you want all the time to change only month number it will break for latest days of this month, same for months with 30 days instead of 31...
You have to think about your approach in another way, because changing the month alone won't make an existing date sometimes.
+30 days seems to be the best thing to do
What Im expecting is 06-30 because you are summing MONTHS not DAYS and if you have an overflow, the code has to correct it, not me.
PHP corrects it, indeed. It never returns 31st of June as such a date doesn't exist. It corrects it to 1st of July.
What you apparently expect is that when you add 1 month to the last day of a month to get the last day of the next month. But this doesn't make any sense.
What should strtotime('2017-06-30 +1 month') return?
2017-07-31, because you are adding 1 month to the last day of June or 2017-07-30 because you are adding 1 month to the 30th day of June?
The times runs forward, counting the days from the end of the month is not natural. Sometimes it's useful but not that many times. And there always is a better solution: subtract 1 day from the first day of the next month. This way you don't have to do any correction or care about months with different number of days or even about leap years.
This is the function I wrote:
//it accept negative month value
public static function realAddMonthsToDate($month,$dateToModify,
$dateFormatInput = DEFAULT_SQL_DATE_FORMAT, $dateFormatOutput = DEFAULT_SQL_DATE_FORMAT)
{
$currentDate = DateTime::createFromFormat($dateFormatInput, $dateToModify);
$cDay = $currentDate->format('d');
$cMonth = $currentDate->format('m');
$cYear = $currentDate->format('Y');
$monthRest = $month;
$yearOffset = 0;
if ($month > 12)
{
$yearOffset = floor($month / 12);
$monthRest = $month - ($yearOffset * 12);
}
$cMonth += $monthRest;
if ($cMonth > 12) {
$cMonth = $cMonth - 12;
$cYear += 1;
}
if ($cMonth <= 0)
{
$cMonth = 12 + $cMonth;
$cYear -= 1;
}
$cYear += $yearOffset;
$arrivalMonthDays = cal_days_in_month(CAL_GREGORIAN, $cMonth, $cYear);
if ($cDay >= $arrivalMonthDays) $cDay = $arrivalMonthDays;
$newDate = new DateTime($cYear.'-'.$cMonth.'-'.$cDay);
return $newDate->format($dateFormatOutput);
}

How to get exact average of n number of order in m number of months?

Example:
$difference = strtotime($to) - strtotime($from);
$months = ($difference / 86400 / 30 );
Problem: But this way I never get exact average. Because I can’t sure for 30 days there can be 31 and 28 days months also.
Even I tried to divide by 12 month average but that also can’t work in every month selection cases
read first and
change according to ur own
You can get number of days for certain month in certain year using this function:
PHP Manual - cal_days_in_month
You can get number of days for whole year using, for example, this solution:
Finding the total number of days in year
Also, if you just want to get number of days for current month, you can use this:
date("t");
Are you after the number of months in a date range? If so, you could modify this previous answer to handle what you want:
PHP: Loop through all months in a date range?
To what I think you're after, you'd do something like this
$date_from = strtotime("2013-08-01");
$date_to = strtotime("2013-10-01");
$month_count = 0;
while ($date_from < $date_to) {
$date_from = strtotime('+1 month', $date_from);
$month_count++;
}
// month_count = number of months covered in the date range
Or, if you're just looking for the number of days in a date range, you could do something like this:
$date_from = strtotime("2013-08-01");
$date_to = strtotime("2013-08-28");
$diff = $date_to - $date_from;
$days_in_range = floor($diff / (60*60*24));
//days_in_range = 27
Not entirely sure what you're after from your question.

Changing hours to days

Editing the question.
I have SQL like this:
`table1`.`DateField` >= DATE_SUB(NOW(), INTERVAL {$days} DAY
Now 24 hours make a whole day. However, what if I want to do the query for the last 3 hours or so?
My table1.DateField is in the format of 2010-03-10 10:05:50.
Original post:
If I have this
1 hour
2 hours
3 hours
..
24 hours
How would I change it to days?
Thanks.
$hours = 80;
$hid = 24; // Hours in a day - could be 24, 8, etc
$days = round($hours/$hid);
if( $days < 0 )
{
echo "$hours hours";
}
else
{
echo "$days days";
}
This assumes you want the hours if it's less than 1 day. If not just remove the switch.
MySQL not only knows DAY as a unit for an interval but also HOUR, MINUTE, ....
see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
$x = 32;
$sql = "SELECT
x,y,z
FROM
foo
WHERE
`table1`.`DateField` >= NOW() - INTERVAL $x HOUR
";
As simple as:
if you want to convert the total of those hour to day:
Just sum the total of hours and that total must be divided by 24
(1 + 2 + 3 + 5) / 24
If you want to convert all of those hours to days:
Just divide by 24 every hours in your list
(1/24) (2/24) (3/24) (5/24)

Categories