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'";
Related
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.
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 tryed to display added date as 25 minutes ago, Today 5.30 PM etc.
$added_time = strtotime('1 Jan 2016 6:00 AM');
$currentTime = strtotime('1 Jan 2016 7:15 AM'); // probably uses time()
$diff = timespan($time1, $time2);
if($diff < 1 hour){ // how to check 1 hour
//display minutes ago
}
else {
//display added time
}
Conditions
if the time gap is less than 60 minute -> 25 minutes ago
If the time gap is over 60 minute But Today -> Today 6.00AM
If the time gap is over 60 minute But Yesterday -> Yesterday 6.00AM
Else exactly $added_time
How to check the condition for less than 1 hour, today and yesterday?
This is not specifically a CodeIgniter question. I am not sure exactly what you are doing, but this code will get you close.
$Added = new \DateTime(date('Y-m-d H:i:s', strtotime('1 Jan 2016 6:00 AM')));
$Current = new \DateTime(date('Y-m-d H:i:s', strtotime('1 Jan 2016 7:15 AM')));
$Diff = $Added->diff( $Current , FALSE);
$hours = $Diff->format('%H');
$mins = $Diff->format('%I');
if( $Diff->invert == true ){
echo "Some hours $hours and minutes $mins ago ";
}
else if( $Diff->invert == false ){
echo "Some hours $hours and minutes $mins into the future ";
}
References:
http://php.net/manual/en/class.datetime.php
http://php.net/manual/en/datetime.diff.php
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'm making a quoting system and I need a way to turn
1 days, 15 hours into 2 days, 7 hours where each day contains 8 hours.
I have access to both the days and hours variables so a function could be
function formatWorkHours($days, $hours){
}
Can anyone help me with this please? Other examples could be 1 days, 26 hours.
Thanks!
Something like this should do the trick:
function formatWorkHours($days, $hours){
$days += floor( $hours / 8 );
$hours = $hours % 8;
echo "$days days, $hours hours";
}
formatWorkHours( 1, 15 ); //echoes "2 days, 7 hours"
formatWorkHours( 1, 26 ); //echoes "4 days, 2 hours"