Compare crop date

I have a field (nonTimeStampDate) that has a date like this

2010-03-15  

and I want to check it on another field (timeStampDate), which

2010-03-15 15:07:45    

to see if the date matches. But, as you can see, since the format is different, it does not match, although the date is the same.
Any help would be appreciated. thank

+3
source share
6 answers

My first thought is date()also used strtotime()to format them.

$date1 ="2010-03-15";
$date2 = "2010-03-15 15:07:45";

if (date('Y-m-d', strtotime($date1)) == date('Y-m-d', strtotime($date2)))
{ 
   //do something 
}

This will work and give you more flexibility in how the two dates begin. Not the most elegant.

+5
source

You might want to try this code:

if (strpos($date1, $date2) !== false)  {
  // Your code here
}

, , Anax. , $date2 .

+4

, , :

$splits = explode(' ', $original);
$datapart = $splits[0];
if ($datepart == $nonTimeStampDate) {
    // your code here
}
+3

What Anax says, or if these values โ€‹โ€‹are listed in MySQL tables, you can use the MySQL datetime functions (like DATE ()) to compare them in MySQL.

+2
source

Then just compare the date:

<?php

if( substr('2010-03-15 15:07:45', 0, 10) == '2010-03-15' ){
    echo 'Dates match';
}

?>

No matter if you need serious data processing, you need to use the correct format, such as an object DateTime.

+2
source
$firstDate = date("Y-m-d", strtotime("2010-03-15"));
$secondDate = date("Y-m-d", strtotime("2010-03-15 15:07:45"));     

if( $firstDate == $secondDate ) {
       // true    
}
+1
source

Source: https://habr.com/ru/post/1757728/


All Articles