how to get days of difference between two dates in php [duplicate] - php

This question already has answers here:
Laravel Carbon date diffInDays() on string error
(7 answers)
Closed 3 years ago.
Hello I want to get days of between two different dates.
$expire_date = ("2019-07-22")
$current_date = Carbon::now()->format('Y-m-d');
$interval = $expire_date->diffIndays($current_date)->days;
dd($interval);
display this error:
Call to a member function diffIndays() on string

Firstly, you'll need to make your $expire_date into a Carbon instance:
$expire_date = Carbon::parse("2019-07-22");
Then diffInDays() already returns the days so you just need to remove ->days. Lastly, don't convert the current date into a string:
$interval = $expire_date->diffIndays(now());

Related

Difference between two timestamp in hh:mm:ss format [duplicate]

This question already has answers here:
calculate the difference between 2 timestamps in php
(7 answers)
How to calculate the difference between two dates using PHP?
(34 answers)
How to get time difference in minutes in PHP
(21 answers)
Closed 3 years ago.
I have two timestamp like below format in my database.
$time1 = 4/19/2019 12:21:01 AM
$time2 = 4/19/2019 12:22:50 AM
I want get difference between this two timestamp like
12:21:01
Let me know if someone can give me idea/solution for do it using php.
You can get the difference as
$time1 = '4/19/2019 12:21:01 AM';
$time2 = '4/19/2019 8:15:01 PM';
$start = new DateTime($time1);
$end = new DateTime($time2);
$diff = $start->diff($end);
print $diff->format("%H:%I:%S");
For more details PHP Manual
So you should have mysql select query like this:
Select time1,time2,time_format(abs(timediff(time1,time2)), "%H:%i") as diff From table1
You can change time format, follow this link for time_format function

Need to change my date format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
How can I change the format of my date from 2016-10-05 to 10/05/2016 in php
$date= $row['date'];
So when the row echos out it'll all be the new format
Use DateTime to create an object (from the current format), allowing you to reformat the output.
$date = "2016-10-05";
$date = DateTime::createFromFormat('Y-d-m', $date);
echo $date->format('d/m/Y');
https://repl.it/DqvO

Compare formatted dates in php [duplicate]

This question already has answers here:
Find difference between two datetimes and format at Y-m-d H:i:s
(7 answers)
Closed 6 years ago.
My question is:
I have two dates in string format:
string(10) "2016-21-10"
string(10) "2016-05-10"
I can not figure out how to compare them. So basically i need the difference between the two given date. How can I achieve it?
You can use createFromFormat() function to create DateTime object from string.
Create two DateTime object like this,
$date1 = DateTime::createFromFormat('Y-d-m', '2016-21-10');
$date2 = DateTime::createFromFormat('Y-d-m', '2016-05-10');
To get the difference between two dates you can use diff() function. Use it like this,
$interval = $date1->diff($date2);
You can use the $interval variable to get the difference between two dates.

Month and Year date conversion [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a Credit card expiry date with month and year only coming in form of a string say 08/17, How can I change this string in a format so that I can pass it to Authorize.net
$creditCard->setExpirationDate( "2017-08");
I have tried to use strtotime() but it is giving me the current year
echo $date = date('Y-m', strtotime($ccInfo['exp_date']));
You should use date_create_from_format instead of strtotime to build your date:
echo date_create_from_format('m/y', '08/17')->format('Y-m');
The function creates a \DateTime object, so you can call format to get the format you want.
You can also write like this
$eventDate = DateTime::createFromFormat('m/y', '08/17');
echo date_format($eventDate, 'Y-m');

Convert date in PHP from number to date [duplicate]

This question already has answers here:
DateTime->format(epoch) returning the wrong date
(4 answers)
Closed 7 years ago.
How do I convert this date: 1427510741 to: 2015-03-22 09:27:02 in PHP?
I tried: $date2 = new DateTime($date1);
but it gives me: 0741-04-08
And I want this: 2015-03-22 09:27:02
You're close. To use a Unix Timestamp with DateTime to must prepend an # before the timestamp:
$date2 = new DateTime('#'.$date1);
Demo

Categories