0

have this count php countdown code i found during my searching on google this is good how ever my code i added to this which is $expire_date shows dec 14 in this format only no time with it.

how do i check if the date is passed and expired. i want it to show like this

if xxxxx date then counting down else expired days :0

$dt_end = new DateTime($expire_date);
$remain = $dt_end->diff(new DateTime());
echo $remain->d . ' days and ' . $remain->h . ' hours';
Sarah Lee
  • 39
  • 5

1 Answers1

0

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. :)

Gianca
  • 481
  • 1
  • 3
  • 9
  • first of all i want to say thats for the effort you made to give me this peace of code . i want to say is it possible to add code to check current month then calculate dec 14th and then se how many days left , because i have some old post that has expired with the dates on them jul 4 ,june xx – Sarah Lee Dec 10 '15 at 16:45
  • To be honest, I didn't understand your request, could you provide a clear example of what you need. – Gianca Dec 10 '15 at 17:59
  • for example my code $expire_date dec 18 and todays date is 12/11/2015 so calculate remaining days till it reaches 18th with min/hours if possible wil be nice – Sarah Lee Dec 11 '15 at 08:14
  • @SarahLee try the script now and in case it's ok please accept the answer. – Gianca Dec 11 '15 at 17:14
  • correct i seen your post above now let me try am happy you understood what i needed thanks and let me try gianca cheers. – Sarah Lee Dec 11 '15 at 21:24
  • @SarahLee I'm happy to help, but don't forget to vote the answer and to flag it like accepted, it could help someone else in future. – Gianca Dec 11 '15 at 22:22
  • is it possible to add live counter rather then showing static time ? i mean when page loads show time counting down ? – Sarah Lee Dec 15 '15 at 12:12
  • Yes of course, but I'll use jQuery to do that. 'll update the first answer. – Gianca Dec 16 '15 at 06:33