Viewed   60 times

How could set a date and get a countdown in PHP? For example if I set the date as 3 December 2PM it would tell me how many days and hours are remaining.

No need for user inputs for the date as it will be hard coded.




You can use the strtotime function to get the time of the date specified, then use time to get the difference.

$date = strtotime("December 3, 2009 2:00 PM");
$remaining = $date - time();

$remaining will be the number of seconds remaining. Then you can divide that number to get the number of days, hours, minutes, etc.

$days_remaining = floor($remaining / 86400);
$hours_remaining = floor(($remaining % 86400) / 3600);
echo "There are $days_remaining days and $hours_remaining hours left";
Sunday, September 11, 2022

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (

  -r, --register-only
        do not remove files, only register the packages as not installed

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022

$target = new DateTime('2011-06-01 05:00:00');
$now = new DateTime;
$diff = $target->diff($now);

if ($target > $now) {
    echo $diff->format('%a days, %h hours, %i minutes to go');
} else {
    echo $diff->format('%a days, %h hours, %i minutes too late');
Friday, August 26, 2022

try this

$date = '2014-03-13T09:05:50.240Z';

$fixed = date('Y-m-d', strtotime($date));

The complete date function documentation can be found here:

The PHP function "strtotime" does nothing else then converting your timestring into an unix timestamp.

Hope I could help :)

P.s.: Just in case strtotime will return 0 try using this:

$date = '2014-03-13T09:05:50.240Z';

$fixed = date('Y-m-d', strtotime(substr($date,0,10)));
Saturday, August 13, 2022

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :