SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Counting down

  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Counting down

    Hey,

    Trying to add 2 weeks to NOW)(), store it in mysql database and then display how manys days and hours are left untill that date arrives for mutiple records. Whats best method to acheive this?

    Thanks

  2. #2
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    If you have access to the DateTime class in PHP, you can calculate the date and store it something like this:
    PHP Code:
    <?php
    date_default_timezone_set
    ('Pacific/Honolulu');
    $now = new DateTime();
    $twoWeeks = new DateTime();
    $twoWeeks->add(new DateInterval('P14D'));

    $sql "INSERT INTO table_name (target_date) VALUES ('" $twoWeeks->format('Y-m-d H:i:s') . "') WHERE id = 1";
    // Code to insert the date/time into whatever place into your DB

    ?>
    Then when you want to display the countdown to a particular entry, you could do something like this:
    PHP Code:
    <?php
    $sql 
    "SELECT target_date FROM table_name WHERE id = 1";
    // Code to pull the date/time from your DB

    $targetDate = new DateTime($dateFromDB);
    $now = new DateTime();



    if (
    $targetDate $now) {
        
    $remaining $now->diff($targetDate);
        echo 
    $remaining->format('%R%a days %R%H hours %R%i Minutes') . " Left\n";
    } else if (
    $targetDate == $now) {
        echo 
    "The target date has arrived!";
    } else {
        echo 
    "You missed it ... it has come and gone.";
    }
    ?>

  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    I tried thats concept but get the following error:

    Fatal error: Call to undefined method DateTime::diff()

    PHP Code:
    $targetDate = new DateTime('2012-06-29 08:25:36'); 
    Is that the correct format for $targetdate?

    Cheers

  4. #4
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is another way I found to acheive same thing.. Im not sure which way would be classed as better?

    PHP Code:

        $inittime
    =time();
        
    $expdate=strtotime($enddate);
        
    $timediff $expdate $inittime;
        
    $days=intval($timediff/86400);
        
    $remaining=$timediff%86400$hours=intval($remaining/3600);
        
    $remaining=$remaining%3600$mins=intval($remaining/60);
        
    $secs=$remaining%60;
        
        if(
    $enddate == '0000-00-00 00:00:00') {
            
    $expires 'Waiting to start';
        }elseif(
    $expdate $inittime) {
            
    $expires ''.$days.' days<br />'.$hours.' hours<br />'.$mins.' minutes';
        }elseif(
    $expdate $inittime) {
            
    $expires 'expired';
        } 

  5. #5
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    You must not be running a version of PHP that supports that class. Oh well, you can do it the old fashioned way.
    PHP Code:
    <?php
        date_default_timezone_set
    ('Pacific/Honolulu');
        
    $now time();
        
    $twoWeeks $now + (60 60 24 14); // 60 seconds * 60 minutes * 24 hours * 14 days = two weeks
        
    $remaining $twoWeeks $now;
        
        
    $days_remaining floor($remaining / (60 60 24));
        
    $hours_remaining floor(($remaining % (60 60 24)) / (60*60));
        
        echo 
    "Today is " date('Y-m-d H:i:s'$now) . "<br />";
        echo 
    "Two weeks from now is " date('Y-m-d H:i:s'$twoWeeks) . "<br />";
        echo 
    "There are " $days_remaining " Days and " $hours_remaining " Hours left."
    ?>

  6. #6
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, altho its PHP5 they are running so I would amadgine it would support unless something is disabled..

  7. #7
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    That class is >= 5.3 so it's possible they don't have it available.

  8. #8
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,014
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Thread moved to databases forum

    You're


    You're probably better off storing the target date of an "event" in the table and in your select query using DATEDIFF() to get the number of days until the event is due. Have a read of this for the various date and time functions available in MySQL


    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  9. #9
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, I think I have a handle on it now..


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •