UPDATED
Try this (based on Armen suggested post)
<?php
$expire_date = "dec 14"; //you can change this date
$datetime1 = new DateTime();
$datetime2 = new DateTime($expire_date);
if($datetime2 >= $datetime1) {
$interval = $datetime1->diff($datetime2);
$remaining_days = $interval->format('%a');
$remaning_hours = $interval->format('%h');
$remaning_minutes = $interval->format('%m');
$remaning_seconds = $interval->format('%s');
echo 'Now is '.date('l jS \of F Y h:i:s A').'<br>';
echo 'To '.$expire_date.' remains ';
echo $remaining_days. ' days, '.$remaning_hours.' hours, '.$remaning_minutes.' minutes, '.$remaning_seconds.' seconds.';
} else {
echo 'Time expired';
};
?>
I've updated the code with your last requests. Try it now... it will stamp something like:
It is Friday 11th of December 2015 06:08:02 PM
To dec 14 remains 2 days, 5 hours, 0 minutes, 58 seconds.
Including a live countdown
To include a live countdown we could use jQuery and a script born to do that. It's very simple, you have just to include jQuery library in your code and then the jQuery library jquery.countdown.min.js and little more.
You could download that library from here:
http://hilios.github.io/jQuery.countdown/
then you have upload it in the same folder of your page on your webserver.
Here the updated code of your page:
<?php
$expire_date = "dec 25"; //you can change this date
$datetime1 = new DateTime();
$datetime2 = new DateTime($expire_date);
$formatted_date = $datetime2->format('Y-m-d'); // we need to pass a well formatted expire date to jQuery
if($datetime2 >= $datetime1) {
$interval = $datetime1->diff($datetime2);
$remaining_days = $interval->format('%a');
$remaining_hours = $interval->format('%h');
$remaining_minutes = $interval->format('%m');
$remaining_seconds = $interval->format('%s');
echo 'Now is '.date('l jS \of F Y h:i:s A').'<br>';
echo 'To Christmas '.$expire_date.' remains ';
echo $remaining_days. ' days, '.$remaining_hours.' hours, '.$remaining_minutes.' minutes, '.$remaining_seconds.' seconds.';
?>
<h2>To Christmas <span id="getting-started"></span></h2> <!-- that is the container of the countdown in your page, jQuery will populate it -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <!-- copy this line as it is, it's load jQuery library from google repository -->
<script src="jquery.countdown.min.js"></script> <!-- here you have to load the countdown library, modify your path in case you'll put that library in a subfolder -->
<!-- that's our jQuery script -->
<script type="text/javascript">
var dateVar = "<?php echo $formatted_date;?>";
var expire_date=new Date(dateVar);
$('#getting-started').countdown(expire_date, function(event) {
$(this).html(event.strftime('%w weeks %d days %H:%M:%S'));
});
</script>
<?php
} else {
echo 'Time expired';
};
?>
OUTPUT
Now is Wednesday 16th of December 2015 07:50:52 AM
To Christmas dec 25 remains 8 days, 16 hours, 0 minutes, 8 seconds. <-- static text
To Christmas 01 weeks 01 days 17:08:48 <-- live countdown
Hoping to be helpful. :)