PHP - Compare Dates with different format - php

Date Format:
$date1 = '16-MAR-2015';
$date2 = '04-FEB-15';
How can I check if
$date1 <= $date2 || $date1 => $date2
Do I need to convert date format in
$date1 = '16-3-2015';
$date2 = '04-2-15';

Use strtotime():
<?php
$date1 = strtotime("16-MAR-2015");
$date2 = strtotime("04-FEB-15");
?>
and compair

Try this way, it works. DEMO
$date1 = DateTime::createFromFormat('j-M-Y', '16-MAR-2015');
$date2 = DateTime::createFromFormat('j-M-y', '04-FEB-15');
$date1=$date1->format('Y-m-d');
$date2=$date2->format('Y-m-d');
var_dump($date1 <= $date2);
var_dump($date1 >= $date2);

You don't need to convert anything with the given formats, DateTime will do what you need.
Just turn them into DateTime objects and compare them :
$date1 = '16-MAR-2015';
$date2 = '04-FEB-15';
$date1 = date_create($date1);
$date2 = date_create($date2);
// $date1 > $date2 -> true
// $date1 < $date2 -> false
Or
if (date_create($date1) > date_create($date2)) {
// Use $date1 unchanged
}

First, you need to turn them into a DateTime object.
DEMO
$date1 = '16-MAR-2015';
$date2 = '04-FEB-15';
$dateComp1 = new DateTime($date1);
$dateComp2 = new DateTime($date2);
Then you can compare them, DateTime is smart enough to convert the format automatically.

$myDateTime = DateTime::createFromFormat('Y-m-d', $dateString); $newDateString = $myDateTime->format('m/d/Y'); There is a magical function called strtotime()
If you need to print the value in a readable format you can use the function DATE
date("d-m-Y h:i:s",$date1);
In your case.
You want to use
Date.parse()

try
date1= new DateTime();
date1= new Datetime->format('d-m-Y'); // this will print year 4 digits ie; 2017
date2= new DateTime();
date2= new DateTime->format('d-m-y'); //this will print 2 digit year ie; 17
// to compare
$differance=$date1->diff($date2);
echo $differance->format('d-m-y'); //or change to upper case Y for 4 digit year
note... in mysql 5.6 it seems the input format must be Y first: if you insert it with Y-first format first, it seems to compare correctly no matter what the second format is.

Related

Check which datetime is older if they have different format PHP [duplicate]

This question already has answers here:
How to Compare Two Date with Time in Php
(2 answers)
Closed 4 years ago.
How to check which date is older if they have different format, for example :
$date1 = "2018-05-09 12:31:51"; // "Y-m-d H:i:s" format
$date2 = "06/09/2018"; // "d/m/Y" format
DateTime::createFromFormat is pretty handy here
Try this:
$date1 = "2018-05-09 12:31:51"; // "Y-m-d H:i:s" format
$date2 = "06/09/2018"; // "d/m/Y" format
$objDateA = DateTime::createFromFormat('Y-m-d H:i:s', $date1);
$objDateB = DateTime::createFromFormat('d/m/Y', $date2);
if ($objDateA < $objDateB)
{
...
}
The easiest way is to use a library called Carbon. (https://carbon.nesbot.com/docs/)
You can do it like this:
use Carbon\Carbon;
$date1 = "2018-05-09 12:31:51";
$date1 = Carbon::createFromFormat('Y-d-m H:i:s', $date1);
$date2 = "06/09/2018"; // "d/m/Y" format
$date2 = Carbon::createFromFormat('d-m-Y', $date2);
if ($date1 < $date2) {
echo "Date 1 is older than Date 2";
else {
echo "Date 1 is newer than Date 2";
}
Try this.
$date1_dt = new DateTime($date1);
$date2_dt = new DateTime($date2);
if ($date1_dt > $date2_dt)
{ /* Do something */ }
It might possible that the date string is not supported by DateTime parser so you can initialize your dates like this.
$date1_dt = DateTime::createFromFormat('Y-m-d H:i:s', $date1);
$date2_dt = DateTime::createFromFormat('d/m/Y', $date2);
if ($date1_dt > $date2_dt)
{ /* Do something */ }

Compare datetime with 0 in laravel

These two datetime string are equal. How can we compare this two string datetime with laravel.
$str_date1='08/09/2017';
$str_date2='8/9/2017';
How to compare these two string datetime by ignore 0 with laravel.
To achieve what you are trying you need Carbon class, you can try something like this:
$date1 = Carbon\Carbon::parse($str_date1)->toDateTimeString();
$date2 = Carbon\Carbon::parse($str_date2)->toDateTimeString();
if($date1 == $date2)
{
// Do your code
}
Hope this helps
You should try this:
$str_date1='08/09/2017';
$str_date2='8/9/2017';
$date1 = strtotime($str_date1);
$date2 = strtotime($str_date2);
if($date1 == $date2){
//Your code
}
Updated answer:
$str_date1='30/08/2017';
$str_date1 = str_replace('/', '-', $str_date1);
$str_date2='30/10/2017';
$str_date2 = str_replace('/', '-', $str_date2);
$date1 = strtotime($str_date1);
$date1 = date('m/d', $date1);
$date1 = strtotime($date1);
$date2 = strtotime($str_date2);
$date2 = date('m/d', $date2);
$date2 = strtotime($date2);
if(date1 == $date2){
//Your code
}
Cast them to DateTime and then compare. No need to use anything like Carbon or any fancy class, or helper.
$str_date1 = '08/09/2017';
$str_date2 = '8/9/2017';
$date1 = \DateTime::createFromFormat('d/m/Y', $str_date1);
$date2 = \DateTime::createFromFormat('d/m/Y', $str_date2);
if ($date1 > $date) {
// Your stuff
}
//Modifys to set same dates
$date1 = new DateTime('08/07/2017')->modify('d-m-Y');
$date2 = new DateTime('8/7/2017')->modify('d-m-Y');
//Difference In Days
$dateDifference = $date1->diff($date2)->format("%a");
Depending on your needs, PHP comes shipped with the ability to differentiate between dates. http://php.net/manual/en/class.datetime.php

Time Subtract in PHP

I need help to subtract 2 times in PHP.
Example:
$date1 = 02:40;
$date2 = 00:00;
$finaldate = $date1 - $date2;
the correct answer will be 21:20.
Check it out:
if use $date2 as 24:00
$date1 = new DateTime('02:40');
$date2 = new DateTime('24:00');
$finaldate = $date2->diff($date1);
echo $finaldate->format('%h:%i'); // 21:20
But if use $date2 as 00:00
$date1 = new DateTime('02:40');
$date2 = new DateTime('00:00');
$finaldate = $date2->diff($date1);
echo $finaldate->format('%h:%i'); //02:40
use strtotime() and subtract both the times ...
<?php
$date1=strtotime("02:40");
$date2=strtotime("00:00");
$diff= date('H:i', $date2-$date1);
echo "Time Difference : ".$diff;
?>
This will output:
Time Difference : 21:20

build a new mysql variable from an existing date field

I have a date column in my MySQL table and I want to build a new variable from that whereby I will add a few days.
example:
$date = 2014-12-12
now I need a second variable $date2 whereby the date will be 2014-12-17
So I need something like this
$date2 = $date + 5 days
I've searched for this and I got solutions for building queries, but I want to have a second variable. Is this possible?
I tried this (with no luck)
$date2 = DateTime::createFromFormat('d-m-Y', $date1);
$date2->modify('5 day');
echo $date2->format('Y-m-d');
This should work:
$date2 = strtotime($date) + (60*60*24*5); //convert date to unix time stamp and add 5 days
$date2 = date('Y-m-d', $date2); //convert back to readable format
Or an even better approach:
$date2 = date('Y-m-d', strtotime($date . "+5 days"));
You are missing the + in your modify call:
$date2 = DateTime::createFromFormat('d-m-Y', $date1);
$date2->modify('+5 day');
echo $date2->format('d-m-Y');

Date Format Conversion in PHP 5.1.2

1.2 and need to convert a date from dd/mm/yyyy to yyyy-mm-dd
For example if the date is in format 07/08/2014, it should appear as 2014-08-07
How can this be done? I know strtotime returns unix timestamp but it doesn't seem to work with dates with Slashes (/) in it. SInce I'm using 5.1, a lot of DateTime functions are not supported in it.
Please help.
Use DateTime class, strtotime function would create issue when date less then 1901 with PHP 5.3.0
Try this way
$date = DateTime::createFromFormat('d/m/Y', "07/08/2014");
$new_date_format = $date->format('Y-m-d');
Need to pass a correct format with -(date string separation with dash) in date() try
$d = str_replace('/', '-','07/08/2014');
echo date('Y-m-d', strtotime($d)); //2014-08-07
with DateTime
$objDateTime = new DateTime($d);
echo $objDateTime->format('Y-m-d'); //2014-08-07
You can do it by date('Y-m-d',strtotime($date))
Where $date is in any format that you want to convert to YYYY-MM-DD format.
By using date() function yuo can try this
echo date('Y-d-m',strtotime('07/08/2014'));
Check the documentation for more
Method : 1 demo
$date1 = "07/08/2014";
$arr = explode("/", $date1);
$date2 = $arr[2]."-".$arr[1]."-".$arr[0];
echo $date2;
Method : 2 demo
$date1 = "07/08/2014";
list($day, $month, $year) = explode("/", $date1);
$date2 = $year."-".$month."-".$day;
echo $date2;
Method 3 : with strtotime Demo
$date1 = "07/08/2014";
$date1 = str_replace("/", "-", $date1);
$date2 = date('Y-m-d', strtotime($date1));
echo $date2;

Categories