Pregunta

Hello i'm trying to make a php script that compares the date from one of my database records and todays date, if the difference in days is greater than 3, it will come out to true.

example:

$todays_date = date("Y-m-d");     <-- Todays date
$deal_date = $data["Deal Date"];  <-- Date from database
$interval = date_diff($todays_date, $deal_date); <--Difference

if($interval >= 3)
{
(something)
} 

but every time i try this i get an error "date_diff() expects parameter 1 to be DateTime, string given" i know that to use date_diff both arguments have to be datetime, but i'm not sure how to get today's date in date and how to convert the date from the database into datetime.

¿Fue útil?

Solución 2

CORRECT SYNTAX IS:

<?php
$datetime1 = date_create('now');
$datetime2 = date_create($data["Deal Date"];);
$interval = date_diff($datetime1, $datetime2);
$diff = $interval->format('%a');
if($diff >= 3)
{
(something)
}



?>

Otros consejos

Try this code:

$date1 = new DateTime('now');
$date2 = new DateTime($data['Deal Date']);
$interval = $date1->diff($date2);
if ($interval->format('%a') >= 3) {
    ...
}

More examples in PHP date_diff documentation.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top