Carbon convert no of days to human readable format - php

I need to convert 30 days to 1 month .If months and days are means then like 1 years 2 month 2 days
I have tried below but it will return wrong result
echo CarbonInterval::days(30)->cascade()->forHumans();
Can any one help me how i can achieve this ?
I have tried below solution but got only 2 days difference
$convert = '30'; // days you want to convert
$years = ($convert / 365) ; // days / 365 days
$years = floor($years); // Remove all decimals
$month = ($convert % 365) / 30.5; // I choose 30.5 for Month (30,31) ;)
$month = floor($month); // Remove all decimals
$days = ($convert % 365) % 30.5; // the rest of days
// Echo all information set
echo 'DAYS RECEIVE : '.$convert.' days<br>';
echo $years.' years - '.$month.' month - '.$days.' days';
Is there any good solution using carbon

Does it have to be CarbonInterval?
What about Carbon::now()->subDays(1827)->diffForHumans()?
The reason it doesn't work as you're expecting (from https://carbon.nesbot.com/docs/#api-interval):
Default factors are:
1 minute = 60 seconds
1 hour = 60 minutes
1 day = 24 hour
1 week = 7 days
1 month = 4 weeks
1 year = 12 months
CarbonIntervals do not carry context so they cannot be more precise
(no DST, no leap year, no real month length or year length
consideration).

Related

PHP strtotime always use 31 days months

I need to calculate a duration that is expressed in human readable format (e.g. "3 months 10 days") and make it a timestamp.
My problem is that with my code uses strtotime, so if a month is 30 days long PHP interprets it as 0 months 30 days instead of 31 days.
What I need is to convert, for example, the string "1 month 10 days" to "31 days 10 days" (or "41 days").
$strToTime = strtotime($pMsg);
$unixTimestamp = $strToTime - time();
$unixTimestampM = $unixTimestamp;
$months = floor((float)$unixTimestampM/2678400);
if($months >= 1) {
$unixTimestampM -= $months*2678400;
}
$days = floor((float)$unixTimestampM/86400);
if($days >= 1) {
$unixTimestampM -= $days*86400;
}
and so on for hours and minutes.
With the string "1 month 10 days" I expect the $months to be 1 and $days to be 10, while, in certain periods of the year, $days become 9.

How to check if there is a weekend between two dates in php?

I want to check if there is any sat or sun between two dates in php.
$fromDate = '03/21/2016';
$toDate = '03/28/2016';
I've tried other solution asked here but they didn't worked well. I want it in laravel 5.2 so if there is any easy way to handle this please guide me. thanks!
Update
i also want to get if there is weekday(mon-fri) between two dates. i need it because user can select sat and Sunday only so in this condition I've to hide weekday option from him. So what I need is a method which tells me if start and end date has weekend or weekdays or both in it.
You can use date("N") to get the current day of the week and add the difference of days between your dates... If this is bigger or equal to 6 than it's a weekend between or one date is in the weekend.
//Get current day of week. For example Friday = 5
$day_of_week = date("N", strtotime($fromDate));
$days = $day_of_week + (strtotime($toDate) - strtotime($fromDate)) / (60*60*24);
//strtotime(...) - convert a string date to unixtimestamp in seconds.
//The difference between strtotime($toDate) - strtotime($fromDate) is the number of seconds between this 2 dates.
//We divide by (60*60*24) to know the number of days between these 2 dates (60 - seconds, 60 - minutes, 24 - hours)
//After that add the number of days between these 2 dates to a day of the week. So if the first date is Friday and days between these 2 dates are: 3 the sum will be 5+3 = 8 and it's bigger than 6 so we know it's a weekend between.
if($days >= 6){
//we have a weekend. Because day of week of first date + how many days between this 2 dates are greater or equal to 6 (6=Saturday)
} else {
//we don't have a weekend
}
try
<?php
$is_weekend = 0;
$fromDate = strtotime('2016-03-21');
$toDate = strtotime('2016-03-28');
while (date("Y-m-d", $fromDate) != date("Y-m-d", $toDate)) {
$day = date("w", $fromDate);
if ($day == 0 || $day == 6) {
$is_weekend = 1;
}
$fromDate = strtotime(date("Y-m-d", $fromDate) . "+1 day");
}
echo $is_weekend;
hoe it helps :)

How to calculate the difference between two dates with time using PHP? [duplicate]

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 ";

php code to convert total number of days to month

I have total number of trial period in a days.
For example, if i set 60 days as a trial period, then it needs to display 2 month
If i set 45 days as a trial period, then it needs to display 1 month and 15 days.
How can we convert to total number of days to month using php?
This code isn't really correct (and month not always have 30 days, but that's like your example is) but it does what you want:
$days = floor($totalDays / 30);
$months = $totalDays % 30;
echo $months . " months and " . $days . " days";

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;
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.

Categories