AKor
AKor

Reputation: 8882

Full Days between two dates in PHP?

I have dates stored in my MySQL table as yyyy-mm-dd (or typical MySQL 'date' format). How can I find out how many full days are left until then?

Like, for example, if I had:

2011-03-05

It would say:

17 More Days

Upvotes: 2

Views: 1167

Answers (3)

Alix Axel
Alix Axel

Reputation: 154513

In PHP:

$days = (strtotime($date) - time()) / 86400;

In MySQL:

SELECT
    ((UNIX_TIMESTAMP(date) - UNIX_TIMESTAMP()) / 86400) AS days
FROM table;

Or as @coreyward stated (in MySQL):

SELECT
     DATEDIFF(UNIX_TIMESTAMP(date,NOW()) AS days
FROM table;

Upvotes: 2

coreyward
coreyward

Reputation: 80041

Try one of these nearly identical questions:

PHP: How to count days between two dates in PHP?

MySQL: How to get the number of days of difference between two dates on mysql?

Upvotes: 0

Kieran Andrews
Kieran Andrews

Reputation: 5885

In PHP 5 - Date DIFF

http://php.net/manual/en/function.date-diff.php

You need: (PHP 5 >= 5.3.0)

If not you can use this function:

<?php
$today = strtotime("2011-02-03 00:00:00");
$myBirthDate = strtotime("1964-10-30 00:00:00");
printf("I'm %d days old.", round(abs($today-$myBirthDate)/60/60/24));
?>

Upvotes: 1

Related Questions