getting hours and day difference between two dates in codeigniter - codeigniter

i am getting two dates as
$date1 = 2020-07-16 03:50:32
$date2 = 2017-01-25 09:43:53
i want to get the difference between thes two dates.
THe difference count hours until 24 hours and then days plus hours.
eg. 2 days and 5 hours.
THe code i tried is this
$createddate = date("d-m-Y H:i:s", strtotime($application['created_at']));
$approvedisapprovedate = date("d-m-Y H:i:s", strtotime($application['approved_at']));
$created = strtotime($createddate);
$approvedisapprove = strtotime($approvedisapprovedate);
$diff = $approvedisapprove - $created;
$days = floor($diff / (60 * 60 * 24));
$hours = round(($diff - $days * 60 * 60 * 24) / (60 * 60));
but it won't work.anybody suggest a solution.

You can use carbon for this. Carbon can make it simple to manipulate dates very efficiently not for only this task but many other related date.
Install carbon
{
"require": {
"nesbot/carbon": "^1.22"
}
}
Usage
$date->diffForHumans();
OR
$date->diffInDays();
In your case, you can try below code -
$createddate = Carbon::parse($application['created_at']);
$approvedisapprovedate = Carbon::parse($application['approved_at']);
//here is carbon magic
$createddate->diffInDays($approvedisapprovedate); //This will give you diff in number of days.
$createddate->diffForHumans($approvedisapprovedate); //This will give you diff which is readable by human. ex- 1 day 2 hours
You can refer Carbon.
Hope this will help for you.

I propose you the DateInterval::format function of php
like this:
$createddate = new DateTime('12-07-2020 12:10:01');
$approvedisapprovedate = new DateTime('16-07-2020 13:15:40');
$interval = $approvedisapprovedate->diff($createddate);
echo $interval->format('%a days %h hours %i minutes')."\n";
It goes back to this:
4 days 1 hours 5 minutes
I don't know the format of the date $application['created_at'] and $application['approved_at'] but I just put it there:
$createddate = new DateTime($application['created_at']);
$approvedisapprovedate = new DateTime($application['approved_at']);
$interval = $approvedisapprovedate->diff($createddate);
echo $interval->format('%a days %h hours %i minutes')."\n";
voici le lien de DateInterval::format :
https://www.php.net/manual/fr/dateinterval.format.php

Related

How to calculate experience by using date with current date in laravel?

I am using laravel framework to develop api's , i have one column inside table with timestamp,i am fetching that value and i want to show that value to the 3 Years 2 Month or if it's been in days it should show 10days, i have tried by using carbon package to convert as per my requirement but i can't able to figure out can you please help me to achieve this one.
Ex1:-
$date = 2022-09-15 00:00:00;
//my expectation is **8days**
Ex2:-
$date = 2021-08-23 00:00:00;
//my expectation is 1 year 1 month
Here is the example you can do like this
$Born = Carbon\Carbon::create(1986, 1,3);
$Age = $Born->diff(Carbon\Carbon::now())->format('%y Year, %m Months and
%d Days');
echo $Age;
Here is your date example is working and it's result
$date1 = \Carbon\Carbon::create("2022-09-15 00:00:00");
$date2 = \Carbon\Carbon::create("2021-08-23 00:00:00");
$totalYearMonthDate = $date1->diff($date2)->format('%y Year,
%m Months and %d Days');
Result:-
1 Year, 0 Months and 23 Days
diffForHumans has parts and minimumUnit options that do what you want:
$options = [
'parts' => 2,
'minimumUnit' => 'day',
'skip' => ['week'],
];
echo Carbon::parse('2022-09-15 00:00:00')->diffForHumans($options) . "\n";
echo Carbon::parse('2021-08-23 00:00:00')->diffForHumans($options) . "\n";
$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "difference ". $interval->y . " years, " .
$interval->m." months, ".$interval->d." days ";
Try doing like this

Calculate hours and minutes from end date and current date in laravel blade?

I want to calculate hours and minutes from end date(stored in my table) and current date in laravel blade.
My blade:
{{\Carbon\Carbon::parse($data['end_date'])->diffForHumans(null, null, null,2)}}
$end_date = \Carbon\Carbon::parse($data['end_date']);
$start_date = \Carbon\Carbon::parse($data['start_date']);
$value = $end_date->diff($start_date,2)->format(' %D days %H hours - %I minutes');
for more details see:
https://www.php.net/manual/en/dateinterval.format.php

PHP Tally of hours is different when cross-checked with Google

I am calculating the sum of hours using PHP inside Laravel. When I cross check the result in Google, the results are different.
Here is my the code in the controller:
$times[] = '7:55';
$times[] = '7:55';
$minutes = 0;
foreach ($times as $time) {
list($hour, $minute) = explode(':', $time);
$minutes += $hour * 60;
$minutes += $minute;
}
$hours = floor($minutes / 60);
$minutes -= $hours * 60;
$tally = sprintf('%02d.%02d', $hours, $minutes);
dd($times, $tally);
The output is: "15.50";
but in google, the output is "15.1".
In Excel and Google Sheets, the output is also "15.50".
Which one is correct and how can I modify my code to reflect the same result as google.
15.1 is wrong, 15h 50m is correct. Time summing is not a mystery, you can check it manually: 7h 55m for example is 8h - 5m, so the sum of the 2 is 16h - 10m = 15h 50m.

Time comparison in html using NGIF Condition - Angular

I am in requirement for solution where I have one Admin posts application for an Android app. I have placed a delete button for post. The requirement is that delete button must be shown for 5 minutes from time of posting.
Here is the ngif condition which I have used..
*ngIf="((((post.CreatedDate | date:'dd/MM/yyyy') == (PresentDate | date:'dd/MM/yyyy')) && ((post.CreatedDate | date:'HH') == (post.CreatedDate | date:'HH')))&&((post.CreatedDate | date:'mm')< (time)))"
Code in TS page for present time + 5 minutes
const d: Date = new Date();
this.PresentDate = d;
var x = new Date();
d.getHours(); // => 9
d.getMinutes(); // => 30
this.time = d.getMinutes() +5;
this.hours = d.getHours();
Please help with the solution
Long expression in html is not good practice.
*ngIf="canDeletePost(post)"
canDeletePost(post) {
return Date.now() - post.CreatedDate.getTime() < 5 * 60 * 1000;
}
If CreatedDate is Js date. 5 * 60 * 1000 - 5 min in milliseconds. Actually, have a method in ngIf is not good practice also.
Anyway, you don't need date pipe. Pipes is used for changing view.

Smarty Date Difference

Please post any smarty syntax to Get number of days between two dates from my database. i was to able to display all the other fields,but this date field with number of days not working as the way i was expected.Please let me know is there any way to get this solution without any smarty additional plugins.
Smarty does not include any specific functions for doing date math operations. They have the date_format for timestamps, but otherwise you'd either have to write your own days_diff plugin, find one online, or do the date math in PHP and assign to a new variable in Smarty.
Here is my function for this problem:
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: date_diff
* Author: RafaƂ Pawlukiewicz
* Purpose: factor difference between two dates in days, weeks, or years
* Input: d1 = "mm/dd/yyyy" or "yyyy/mm/dd" or "yyyy-mm-dd"
* d2 = "mm/dd/yyyy" or "yyyy/mm/dd" or "yyyy-mm-dd" or $smarty.now
* assign = name of variable to assign difference to
* interval = "days" (default), "weeks", "years"
* Examples: {date_diff d1="2020-01-20"}
* Examples: {date_diff d1="2020-01-20" d2=2020-02-10 interval="weeks"}
* Examples: {date_diff d1="2020-01-20" d2=2020-02-10 assign="variable_diff"} result: {$variable_diff}
* -------------------------------------------------------------
*/
function smarty_function_date_diff($params, &$smarty)
{
$d1 = isset($params['d1']) ? $params['d1'] : date('Y-m-d');
$d2 = isset($params['d2']) ? $params['d2'] : date('Y-m-d');
$assign_name = isset($params['assign']) ? $params['assign'] : '';
$date1 = strtotime($d1);
$date2 = strtotime($d2);
// use current for empty string
if (! $date1) {
$date1 = date('Y-m-d');
}
if (! $date2) {
$date2 = date('Y-m-d');
}
$interval = isset($params['interval']) ? $params['interval'] : 'days';
// diff in days
$diff = ($date2 - $date1) / 60 / 60 / 24;
if ($interval === "weeks") {
$diff /= 7;
}
elseif ($interval === "years") {
$diff /= 365.25;
}
$diff = floor($diff);
if ($assign_name) {
$smarty->assign($assign_name, $diff);
}
else {
return $diff;
}
}

Resources