Days elapsed between php dates

1

hello friends I am programmed a system in which they ask me to show the days elapsed between two dates with Y-m-d format that would be the current date - the date on which the entry was made to the bd and I found the problem I do not calculate the days if only the rest of the current - the income does not give me anything I've tried in various ways and nothing

//primera forma
$datetime1 = date_create($fecha_actual);
$datetime2 = date_create($fecha_db);
$interval = date_diff($datetime1,$datetime2);
echo $interval->format('%R%a dias');

// segunda forma
$today = new DateTime($fecha_actual);
$appt  = new DateTime($fecha_db);
echo  $days_until_appt = $appt->diff($today)->d;

// tercera forma
$fecha1 = date($fecha_actual);
$fecha2 = new Date($fecha_db);
echo  $fechaF = $fecha->diff($fecha2);
    
asked by json 30.06.2017 в 20:11
source

2 answers

1

You can do a SQL query to the database using the functions DATEDIFF (returns the difference of days between two dates) and CURDATE (returns the current date).

// Diferencia de días entre la fecha de la tabla y la fecha actual
SELECT DATEDIFF( CURDATE(), fecha ) AS Dias FROM tabla;

 __________
| Dias     |
|----------|
| 7        |
|__________|
    
answered by 30.06.2017 в 21:00
1

You can try converting dates to timestamp, subtracting them and formatting them later.

$fecha1 = strtotime($fecha1);
$fecha2 = strtotime($fecha2);

$res = $fecha2 - $fecha1;

$dias = date('d', $res);

PHP Documentation:

String to time

Date

    
answered by 30.06.2017 в 21:33