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
Related
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 */ }
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.
How can I reformat/split the datetime data (from a database) into two variables, date and time, and then add them back to the array?
At present $datetime contains data such as: 2014-12-03 00:00:00
I want to split it out into $date and $time
I have the following, but when I echo out the $date and $time vars they are empty:
$datetime = date($data['date_time']);
$date = date_format($datetime,"Y-m-d'");
$time = date_format($datetime,"H:i");
DateTime objects are created using date_create(), not date()
$datetime = date_create($data['date_time']);
$date = date_format($datetime,"Y-m-d");
$time = date_format($datetime,"H:i");
Why not simply do it using DateTime, in OOP way
$date = new DateTime('2000-12-31 00:00:00');
$dateonly=$date->format('Y-m-d');
$timeonly=$date->format('H:i');
DEMO
With your current code you can try this :
$datetime = date($data['date_time']);
$dateArray = explode($datetime, " ");
$date = $dateArray[0];
$time = $dateArray[1];
I would do:
$datetime = new DateTime($data['date_time']);
$date = $datetime->format('Y-m-d');
$time = $datetime->format('H:i:s');
You can use strtotime:
$datetime = date($data['date_time']);
$date = date("Y-m-d",strtotime($datetime));
$time = date("H:i:s",strtotime($datetime));
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;
I am having trouble writing if statement
there are the 3 variables
$date = 1985-11-01;
$date2 = 2005-11-08;
$date3 = 2006-11-08;
and here is my if statement.
if($date > $date2 && $date < $date3) {
// dob is between the limits
return TRUE;
}
else {
// dob is outside the limits
return FALSE;
}
What I am try to do is, if $date is not in between $date2 and $date3, return false. I am very tired today and my brain is not working, can someone tell me what I am doing wrong?
You can use strtotime to ensure you compare correctly.
$date = strtotime('1985-11-01'); //499680000
$date2 = strtotime('2005-11-08'); //1131436800
$date3 = strtotime('2006-11-08'); //1162972800
When you do your logic, look at the Unix generated timestamps...
convert your variables to time objects using strtotime
$my_date = strtotime('08/11/2012');
//using strtotime convert your date(s) in time stamp then your checking will be correctly worked.
$date = strtotime('1985-11-01');
$date2 = strtotime('2005-11-08');
$date3 = strtotime('2006-11-08');
if($date > $date2 && $date < $date3)
return true;
else
return false;