Results 1 to 9 of 9

Thread: date problem again

  1. #1
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy date problem again

    hi i want to find to display the the date 1 week befor eand the date 15 days before the current date..
    i have used $lastweek = mktime(0,0,0,date("m"),date("d")-7,date("Y")); for date 1 week bk and $last15 = mktime(0,0,0,date("m"),date("d")-15,date("Y"));
    but i dont think tht is correct.. cos if my current date is 5-5-2007 then last week should become 28-4-2007.. similarly he date for 15 days bk..
    can some1 pls help me with this?
    thanks in advance..suk

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    one week ago: time()+7*24*60*60
    15 days later: time()+15*24*60*60
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    shouln't it be
    one week ago: time()-7*24*60*60
    15 days later: time()-15*24*60*60

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ah, right. Always the little things....

    Anyway, that work?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    well yes but lemme expand my prob..
    the dates are stored in my database as dd/mm/yyyy format
    i want to retieve record according to the date de have been posted
    i hv my current date as date("d/m/Y") which gives me 29/05/2007
    say today is 05/05/2007
    now if i want records posted in last 7 days i want records posted on and after 28/04/2007
    so will the previous code work?

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Storing dates in a database for any other reason that for people to read later is a really bad idea. Just use timecodes in the database and this is all very simple.
    timecodes are ugly, but you get used to them fast, and there's nothing easier than math with timecodes, especially compared with using real dates.

    If you must do what you describe, then just use date('..',$timestamp), where $timestamp is the time created above.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i dont really understand what u mean..i hv an application form which i hv to maintain by the dates the application was filled..how do i do tht with timecode.. jus time()??

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    time() is the current time.
    mktime() can do it from a given date.
    date() can take a timecode (as second parameter) and output (defined by first parameter) as human-readable date.

    Not sure why your application does this, but you could convert before entering it into the mysql db, or later convert.

    It is still possible, though more complex and with extra steps to, as I said above, use a mix of date() and mktime() to get to this same result.

    I'm not sure about the particular situation.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    May 2007
    Location
    Sweden
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    Hi all!

    Here are a code i wrote for a administrationpanel about 3 month ago.
    You use it as following, 15 days from now: calcTime("15 days"), 15 days ago from now: calcTime("15 days", "-"), also if you want get 1 years from now: calcTime("1 year") and calcTime("3 years"), calcTime("1 week", "-") and so on.

    PHP Code:
        function calcTime$t '1 day'$p '+' )
        {

        
    $time time();

        
    $t preg_replace(array("/\s/""/\n/"), ""trim($t));

            if( 
    preg_match("/(\d{1,3})(sec|second|seconds|min|minute|minutes|hour|hours|day|days|week|weeks|month|months|year|years)/"$t$r) )
            {

            
    $num  = ( !is_numeric($r[1]) ? $r[1] );
            
    $what $r[2];

                switch( 
    $what )
                {

                    case 
    'year':
                    case 
    'years':

                        if( 
    $p == '-' )
                        return ( 
    $time - ( ( 60 60 24 365 ) * $num ) );
                        else
                        return ( 
    $time + ( ( 60 60 24 365 ) * $num ) );

                    break;

                    case 
    'month':
                    case 
    'months':

                        if( 
    $p == '-' )
                        return ( 
    $time - ( ( 60 60 24 30 ) * $num ) );
                        else
                        return ( 
    $time + ( ( 60 60 24 30 ) * $num ) );

                    break;

                    case 
    'week':
                    case 
    'weeks':

                        if( 
    $p == '-' )
                        return ( 
    $time - ( ( 60 60 24 ) * $num ) );
                        else
                        return ( 
    $time + ( ( 60 60 24 ) * $num ) );

                    break;

                    case 
    'day':
                    case 
    'days':

                        if( 
    $p == '-' )
                        return ( 
    $time - ( ( 60 60 24 ) * $num ) );
                        else
                        return ( 
    $time + ( ( 60 60 24  ) * $num ) );

                    break;

                    case 
    'hour':
                    case 
    'hours':

                        if( 
    $p == '-' )
                        return ( 
    $time - ( ( 60 60 ) * $num ) );
                        else
                        return ( 
    $time + ( ( 60 60 ) * $num ) );

                    break;

                    case 
    'sec':
                    case 
    'second':
                    case 
    'seconds':

                        if( 
    $p == '-' )
                        return ( 
    $time $num );
                        else
                        return ( 
    $time $num );

                    break;

                    case 
    'min':
                    case 
    'minute':
                    case 
    'minutes':
                    default:

                        if( 
    $p == '-' )
                        return ( 
    $time - ( 60 $num ) );
                        else
                        return ( 
    $time + ( 60 $num ) );

                    break;

                }


            }
            else
            {
                if( 
    $p == '-' )
                return ( 
    $time - ( ( 60 60 24 ) * ) );
                else
                return ( 
    $time + ( ( 60 60 24  ) * ) );
            }

        } 
    Ex: One month ago,
    PHP Code:
        $ctime calcTime("1 month""-");
        echo 
    "1 month ago is: " date("d/m/Y"$ctime); 
    Questions?

    Best regards,
    mbrodin
    Last edited by mbrodin; 05-29-2007 at 09:14 AM.

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
  •