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

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

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

Converting date to mothname, day, year with like() codeigniter 4

I'm using ci4 and I want to get the month name on search with like() method:
I tried this but did not work:
table column = p.date = '2021-05-18 01:07:50'
$this->like(date('F', strtotime('p.date')), date('F', strtotime('18-05-2021'));
//createdon - 2020-06-15 02:19:40
$this->db->where('month(createdon)',6);
$result = $this->db->get('yourtablename')->result();

getting hours and day difference between two dates in 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

How to split date and time from a string like 8/29/2011 1:00 PM in laravel

in my blade there is a DateTime picker which selects in mm/dd/yyyy hh:mm AM/PM format. and i need to extract this to date = 8/29/2011 and time = 13:00 .
how can I do that?
Do as below.
$stringdate = "8/29/2011 1:00 PM";
$timestemp = strtotime($stringdate);
$date = date('Y-m-d', $timestemp);
$time = date('H:i:s',$timestemp);
echo $date;
echo $time;
check example
Edit:- as you want this format YYYY-MM-DD hh:mm
$datetime = date('Y-m-d H:i', strtotime("8/29/2011 1:00 PM"));
echo $datetime; //output "2011-08-29 13:00";
Try this way.
#php
$input = '2017-06-27 12:58:20';
$format1 = 'Y-m-d';
$format2 = 'H:i:s';
$date = Carbon\Carbon::parse($input)->format($format1);
$time = Carbon\Carbon::parse($input)->format($format2);
#endphp
It is simple just try it
$date="8/29/2011 1:00 PM"; // date that get from view (blade page)
$createTimestamp = new DateTime($date); // create timestamp object from string
$date = $createTimestamp->format('m/d/Y'); // get date
$time = $createTimestamp->format('H:i:s A'); // get time with AM/PM format
return "date: ".$date." time: ".$time; // return your result
Try this way
Use Carbon;
$dateAndTime=Carbon::parse('8/29/2011 1:00 PM');
$date=$dateAndTime->format('d-m-Y');// 29-8-2011
$time=$dateAndTime->format('H:i:s');// 1:00
$time=$dateAndTime->format('H:i:s A');// 1:00 AM/PM
Refer this link to understand more about Carbon.

Retrieving date format with am/pm in codeigniter

I have converted date to my local time as below:
$this->date_string = "%Y/%m/%d %h:%i:%s";
$timestamp = now();
$timezone = 'UP45';
$daylight_saving = TRUE;
$time = gmt_to_local($timestamp, $timezone, $daylight_saving);
$this->updated_date = mdate($this->date_string,$time);
And I'm storing this field in to database.
Now at retrieval time I want format like this:
"11-04-2011 4:50:00 PM"
I have used this code:
$timestamp = strtotime($rs->updated_date);
$date1 = "%d-%m-%Y %h:%i:%s %a";
$updat1 = date($date1,$timestamp);
But this will give me only
"11-04-2011 4:50:00 AM"
But I have stored it like it was PM.
Might get voted down, but will have a go at it.
Is it because the MySQL stores it in 24 hour format? (assuming you are using the datetime field type)
Maybe this will help
Converting mysql TIME from 24 HR to AM/PM format
sorry if it doesn't.

Resources