Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Time Left

  1. #1
    New Coder
    Join Date
    Apr 2005
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Time Left

    Alright, so entires on my site stay up for one week. So, I wish to tell the user how many days left until the entry expires. What I have now works, but I can see it causing major problems in the future. I need something that will be more precise. The date the entry was created is stored in the database.

    PHP Code:
    $now date('d',strtotime("now"));
    $left date('d',strtotime($date));
    $left $now $left;
    $left $left
    Thanks!

  • #2
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    PHP Code:
    $date '2005-06-20';

    $timestamp_date strtotime($date);
    $timestamp_week_ago strtotime('7 days ago 00:00:00');

    if (
    $timestamp_date $timestamp_week_ago)
    {
        
    $diff $timestamp_date $timestamp_week_ago;
        echo 
    $diff 86400 ' days left';
    }
    else
    {
        echo 
    'expired';


  • #3
    New Coder
    Join Date
    Apr 2005
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hrmmm, it seems to spit out a pretty long value, and for some reason - it also seems to be wrong.

    For an entry with 7 days left, it says it has "Days of Voting Left: 7.5016898148148"

    7.5 would be more than 7, maybe I'm doing something wrong.

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you storing the timestamp in the database? If so, try it this way:
    PHP Code:
    <?php
    $days 
    7// how many days to show for
    $timepassed time() - $date// $date is the original date from your database, it should be a timestamp format
    $dayspassed = ((($timepassed 60) / 60) / 24);
    if(
    $dayspassed $days)
        echo (
    $days floor($dayspassed)) . ' days left';
    } else {
        echo 
    'Time up';
    }
    ?
    The floor() function should make sure that only whole days are shown so you won't get the 7.5016898148148 days problem.

    If you're not storing the time but instead storing a date format, you will need to use strtotime to convert this first.
    Last edited by delinear; 06-24-2005 at 10:30 PM.


  •  

    Posting Permissions

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