PHP Countdown to Date

PHP Countdown to Date

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";

making a simple countdown clock in php with variable end time

Use DateTime instead, it's much easier:

$future = new DateTime();
$future->setTimestamp(1445438099); // passing in to constructor directly is wonky
$start = $future->sub(new DateInterval('P10D'));
$diff = $start->diff(new DateTime());
$interval = $diff->format('%y:%m:%d:%h:%i:%s');
// 0:1:20:23:29:45
list($year, $month, $day, $hour, $minute, $second) = explode(':', $interval);

How to make a countdown using PHP

If your need your counter to be displayed only on page refresh and be static once the page is loaded, then PHP will be fine.

If you need the countdown to get refreshed when the page is displayed, you'll need to use JavaScript.

Personally I would go for something already implemented, like that small script.

How to make PHP countdown with am/pm stored in value?

You can use the PHP DateTime class to convert the date to the required format:

<?php
// The date for 'poll_end_date'
$date = 'February 9, 2018 3:47 pm';

// Parse the string based on the expected format
$dtime = DateTime::createFromFormat("F j, Y g:i a", $date);

// Output the date to match the required format
$timestamp = $dtime->format('F j, Y H:i');
?>

You can then use this PHP variable inside your usual <script></script> tags.

<script>
var countDownDate = new Date("<?php echo $timestamp; ?>").getTime();
// ... your script continues below
</script>

The methods are well documented here:

  • DateTime::createfromformat()
  • DateTime::format()

You can read up on how the date formats such as F j, Y H:i works on this page.

jquery countdown invalid date countdown

The countdown starts. But instead of showing me 1 month and 12 days, it just shows me 1 day.

The issue is that you have used %d for "days" - you need to use %D for number of days

From: http://hilios.github.io/jQuery.countdown/documentation.html

%d %-d Days left (taking away weeks)

%D %-D Total count of days till the end

so you would use %d in combination with %w (1 week 1 day) or $D without %w (8 days)

CountDown timer PHP/JavaScript

EDIT

Internet Explorer and Safari aren't tolerant on malformed dates.

The date passed from PHP to JavaScript Date() : 2017-09-30 00:00:00 was "malformed".
It came from PHP Date("Y-m-d H:i:s"); which is VERY commonly used...

The fix on JavaScript side is: date = date.replace(" ","T");

It also could be fixed on PHP side with: $date = Date("Y-m-d\TH:i:s");
Or if the date comes form a database:

$date = str_replace(" ","T",$row['time_to_expire']);
createCountDown("form<?php echo $row['id'];?>", "<?php $date;?>")

The resulting date string is then 2017-09-30T00:00:00, which is ISO 8601 compliant.

The issue was abvout ONE character!
I'll remember that one.
;)



Related Topics



Leave a reply



Submit