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.
Related
This question already has answers here:
Converting string to Date and DateTime
(13 answers)
Convert one date format into another in PHP
(17 answers)
Closed 3 years ago.
I'm trying to convert a string of numbers I am pulling from a db that looks like this '010219', representing January 2, 2019. I cannot find a way to convert this into 2019-01-02 using php, I just keep getting today's date from the functions I am trying.
Needs to be accomplished with no separators in original string.
There are a variety of ways to accomplish this, however the most concise is probably to use date_create_from_format.
An example is here:
$date = date_create_from_format('dmy', '010219');
This will output a Date as so:
echo date_format($date, 'Y-m-d');
Outputs: 2019-02-01
The date_create_from_format function accepts a parameter that defines the format of the date. In this case, the format is dmy which means:
d - Day of month as two-digit number (01-31)
m - Month of year as two-digit number (01-12)
y - Year as two-digit number
The documentation for date_create_from_format is here.
have you tried something like this?
<?php
$str="010219";
$date = DateTime::createFromFormat('dmy', $str);
echo $date->format('Y-m-d'); //2019-02-01
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
echo $newformat;
// 2003-10-16
Please see source: Converting string to Date and DateTime
split and concatenate with preg_replace
$newformat = preg_replace("/^(\d\d)(\d\d)(\d\d)$/","20$3-$1-$2","010219");
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());
This question already has answers here:
Find difference between two datetimes and format at Y-m-d H:i:s
(7 answers)
time difference in HH:MM:SS format
(2 answers)
Closed 5 years ago.
I am trying to find the time difference between two time values in the format H:mm:ss using PHP.
When I tried with the following code, I'm getting the difference 01:00:20 instead of 00:00:20. What's wrong with my code?
$start_time = strtotime("0:17:14");
$end_time = strtotime("0:17:34");
$diff = $end_time - $start_time;
echo date('H:i:s', $diff);
Your $diff variable is not a timestamp, it's a duration/interval. The date() function is intended to format timestamps, and won't properly handle intervals like you're expecting.
Instead, try using the DateTime class to read your timestamps, and turn the difference between them into a DateInterval using DateTime::diff(). You can then use DateInterval::format to get the output you want.
Something like this should work:
$start_time = new DateTime("0:17:14");
$end_time = new DateTime("0:17:34");
$diff = $end_time->diff($start_time);
echo $diff->format('%H:%I:%S');
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');
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have the following datetime 2014-02-05 17:12:48 stored in a php variable named $reportDb["reportDate"].
Now, It is in Y-m-d H:i:s format, I want it in d/m/Y H:i:s format. How can I set or reformat this variable?
I tried with datetime::createformat but It doesn't works.
$formatted = date("d/m/Y H:i:s", strtotime($reportDb["reportDate"]))
If you want another method. Even if i would prefer the one already mentioned.
Output 05/02/2014 17:12:48
PHP 5.3 and older:
$dt = new DateTime('2014-02-05 17:12:48');
echo $dt->format(''d/m/Y H:i:s'');
PHP 5.4+:
$dt = (new DateTime('2014-02-05 17:12:48'))->format(''d/m/Y H:i:s'');
with the variable:
$dt = (new DateTime($reportDb["reportDate"]))->format(''d/m/Y H:i:s'');
If I understand you right, $reportDb["reportDate"] is already a DateTime object.
If so, all you need is ...
$reportDb["reportDate"]->format('d/m/Y H:i:s');
Cheers!