Page 1 of 1
Date formatting
Posted: Fri Feb 15, 2008 11:17 am
by dannyd
Heres my problem.
I have a directory with files that have dates in their names such as 080202.txt ( which is really YYDDMM)
Im able to break the filename into $YEAR, $DAY, $MONTH variables but then im stuck.
$YEAR = 2007
$DAY = 02
$MONTH = 02
How can I compare these variables with todays date and grab the last 3 days files and output them to the screen ? The next step would be to insert them which im sure i can figure out.
Re: Date formatting
Posted: Fri Feb 15, 2008 11:34 am
by danielfs1
im pretty sure you can use the php function date() to get the current time.
use this page
http://us.php.net/date to learn how to format it.
then finding previous days date are as easy as:
Code: Select all
<?php
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
?>
Re: Date formatting
Posted: Fri Feb 15, 2008 11:51 am
by anjanesh
Code: Select all
$Now = strtotime("now");
$ThreeDaysAgo = strtotime("-3 day");
$Then = mktime(0, 0, 0, $DAY, $MONTH, $YEAR);
if ($ThreeDaysAgo <= $Then && $Then <= $Now)
{
// Get File
}
Re: Date formatting
Posted: Fri Feb 15, 2008 12:16 pm
by dannyd
Thanks! Any idea what I could be doing wrong in my loop ? Im trying to output the dates in the last 3 days with format yyddmm:
$today = date("ydm");
$CURRENT_YEAR = substr($today,0,2);
$CURRENT_DAY = substr($today,2,2);
$CURRENT_MONTH = substr($today,4,2);
for ($i=1;$i<4;$i++){
$past_date= mktime(0, 0, 0, date($CURRENT_MONTH), date($CURRENT_DAY)-$i, date($CURRENT_YEAR));
echo $i . ' days ago' . $past_date . '<BR>';
}
Re: Date formatting
Posted: Fri Feb 15, 2008 12:36 pm
by dannyd
$today = date("ydm");
$CURRENT_YEAR = substr($today,0,2); //08
$CURRENT_DAY = substr($today,2,2); //15
$CURRENT_MONTH = substr($today,4,2); //02
for ($i=1;$i<4;$i++){
$past_date = mktime(0,0,0,$CURRENT_MONTH,$CURRENT_DAY-$i, $CURRENT_YEAR);
echo $i . ' days ago' . $past_date . '<BR>';
}
Any idea why my output looks like this ?
1 days ago1202965200
2 days ago1202965200
3 days ago1202965200
Re: Date formatting
Posted: Fri Feb 15, 2008 12:41 pm
by danielfs1
try something like this
$date = date("n/d/y g:ia", mktime($hour,$minute,$second,$month, $day, $year));
the first part will format it for you
Re: Date formatting
Posted: Fri Feb 15, 2008 12:48 pm
by RobertGonzalez
Try this...
Code: Select all
<?php
$thisday = date('d');
$thismonth = date('m');
$thisyear = date('y');
$today = (string) $thisyear.$thismonth.$thisday;
for ($i = 1; $i < 4; $i++) {
$past = date('ymd', strtotime("-$i days ago"));
echo "$i days ago: $past";
}
?>
See if that works.
And try to remember to wrap your code in either code or php bbCode tags.
Re: Date formatting
Posted: Fri Feb 15, 2008 3:38 pm
by dannyd
Awesome thanks!