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.
Related
If I have
$time_interval = date_diff(date1, date2)
How can I do this in PHP?
If ($time_interval >= ('2 months and 15 days'))
echo "time is '2 months and 15 days' or more"
else
echo "time is less than '2 months and 15 days'"
I tried
if ($time_interval->m <= 2 and $time_interval->d < 15)
But this will return FALSE for 1 month and 20 days which is obviously wrong
Is there something like..?
$time_lapse = create_my_own_time_lapse(2months & 15 days)
Then it would be very neat to compare both
If ($time_interval >= $time_lapse)
SOLUTION
date_diff retuns a DateInterval object. I found the way to create my own DateInterval for '2 months and 15 days'. This is my updated code:
Visit PHP DateInterval manual for details
$today = new DateTime(date('Y-m-d'));
$another_day = new DateTime("2019-05-10");
$time_diff = date_diff($today, $another_day);
// 'P2M15D' is the interval_spec for '2 months and 15 days'
$time_interval = new DateInterval('P2M15D');
// Let's see our objects
print_r($time_diff);
print_r($timeInterval);
if($time_diff >= $time_interval)
echo "<br/>time is '2 months and 15 days' or more";
else
echo "<br/>time is less than '2 months and 15 days'";
your code is almost correct. Just remove the and and add strtotime()
from:
if ($time_interval >= ('2 months and 15 days'))
echo "time is '2 months and 15 days' or more";
else
echo "time is less than '2 months and 15 days'";
to:
if ($time_interval->getTimestamp()) >= strtotime('2 months 15 days'))
echo "time is '2 months and 15 days' or more";
else
echo "time is less than '2 months and 15 days'";
Easy way of doing this is converting your time to seconds and than compare these seconds with amount of seconds equal to 2 months and 15 days.
$timeInterval = strtotime('2009-12-01') - strtotime('2009-10-01');
$overSeconds = 60 * 60 * 24 * 75; // 60 seconds * 60 minutes * 24 hours * 75 days
if($timeInterval >= $overSeconds)
echo "time is '2 months and 15 days' or more";
else
echo "time is less than '2 months and 15 days'";
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).
I am using this function to compare a date from the database to the current date, and i need to check if the difference between the 2 dates is bigger than 15 minutes but i don't know how to do that, i think i need to do something like if($comp > 0 days 0 hours 15 minutes)
function TimeOut($dateP){
$date = new DateTime(date('Y-m-d H:i:s'));
$date2 = new DateTime($dateP);
echo $comp = $date->diff($date2)->format("%d days %h hours and %i minuts %s seconds");
if ($comp > "15 minutes ?") {
return true;
}
}
You can use diff and then read the m parameter of the result. In the example below $difference will be DateInterval object:
$difference = $start_date->diff($date2);
if($difference->i > 15) {
echo "difference greater than 15 minutes"
}
A date interval stores either a fixed amount of time (in years,
months, days, hours etc) or a relative time string in the format that
DateTime's constructor supports.
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 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.