Strtotime - whole days when dividing by 86400

Firstly I am a copy and paste programmer, nowhere near an expert.

My Code
$date = date(“Y-m-d”); // Todays Date
$date_unix = strtotime($date); // Todays date as Integer
$launchdate_unix = strtotime($launchdate); //Launch Date as Integer
$days_since_launch = ($date_unix - $launchdate_unix) / 86400; // Days since

date 2020-11-23
date_unix 1606107600
launchdate 2020-10-18
launchdate_unix 1602993600
days_since_launch 36.041666666667

I need days_since_launch to be an integer irrespective of what time of day $date is calculated

Any suggestions welcome

Both php and MySql have date diff functions that return the number of days between two values -


$launchdate = '2020-10-18';

// using php datetime objects

$datetime1 = new DateTime($launchdate);
$datetime2 = new DateTime(); // 'now'
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days'); // with a +/- sign
echo $interval->format('%a days'); // just the days
echo $interval->format('%a'); // just the number

// assuming the launchdate is stored in a database table
// do this in a query
require 'pdo_connection.php';

// the following would be part of what you are SELECTing in a query
$sql = "SELECT DATEDIFF(NOW(), '$launchdate')"; // replace '$launchdate' with the column holding the value
$stmt = $pdo->query($sql);
echo $stmt->fetchColumn();

The correct answer is to use the date functions, either with native PHP or with database functions, or Carbon::class. The quick answer is to use int_div( int $dividend , int $divisor ). floor() and ceil() both return floats.

Sponsor our Newsletter | Privacy Policy | Terms of Service