John-bosco Dukor
John-bosco Dukor

Reputation: 11

Subtracting between date to get remaining number of days using php without using date_diff

I have two variables holding different dates in the database one is date created and the other is expiry date. How can I subtract the first date from the other so that I can return the remaining days using phpmysql.

Upvotes: 0

Views: 82

Answers (2)

vural
vural

Reputation: 391

Firstly use strtotime for each dates to convert Unix timestamp.

$date_created = strtotime($date_created);
$date_expired = strtotime($date_expired);

Then get the second differences and multiply the results to a day's seconds.

$date_diff = floor(($date_expired - $date_created) / (60 * 60 * 24));
echo "Date diff: " . $date_diff . " days";

I didn't try codes. Hope it works.

Upvotes: 0

MustDie1Bit
MustDie1Bit

Reputation: 561

I think your dates stores in datetime fields. So you can find diff in such way:

   $seconds = strtotime( $expired ) - strtotime( $created );
   /*** get the days ***/
   $days = intval($seconds / (60 * 60 *24));

Ofc, you can use DateTime for your purposes

Upvotes: 1

Related Questions