Date formatting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

Date formatting

Post 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.
danielfs1
Forum Newbie
Posts: 13
Joined: Thu Feb 14, 2008 10:37 am
Location: Georgia

Re: Date formatting

Post 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);
?>
 
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Re: Date formatting

Post 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
 }
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

Re: Date formatting

Post 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>';

}
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

Re: Date formatting

Post 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
danielfs1
Forum Newbie
Posts: 13
Joined: Thu Feb 14, 2008 10:37 am
Location: Georgia

Re: Date formatting

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Date formatting

Post 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.
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

Re: Date formatting

Post by dannyd »

Awesome thanks!
Post Reply