PDA

View Full Version : date problem again



sukanya.paul
05-29-2007, 04:28 AM
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

djr33
05-29-2007, 04:35 AM
one week ago: time()+7*24*60*60
15 days later: time()+15*24*60*60

sukanya.paul
05-29-2007, 04:40 AM
shouln't it be
one week ago: time()-7*24*60*60
15 days later: time()-15*24*60*60

djr33
05-29-2007, 04:44 AM
Ah, right. Always the little things....

Anyway, that work?

sukanya.paul
05-29-2007, 04:49 AM
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?

djr33
05-29-2007, 04:55 AM
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.

sukanya.paul
05-29-2007, 04:58 AM
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()??

djr33
05-29-2007, 05:06 AM
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.

mbrodin
05-29-2007, 08:39 AM
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.


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]) ? 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 * 7 ) * $num ) );
else
return ( $time + ( ( 60 * 60 * 24 * 7 ) * $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 ) * 1 ) );
else
return ( $time + ( ( 60 * 60 * 24 ) * 1 ) );
}

}

Ex: One month ago,


$ctime = calcTime("1 month", "-");
echo "1 month ago is: " . date("d/m/Y", $ctime);


Questions?

Best regards,
mbrodin