date

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
nmotion
Forum Commoner
Posts: 32
Joined: Wed Jan 22, 2003 6:53 am

date

Post by nmotion »

I'm new to PHP so this may be a stupid question but here goes.

I want to take the current date and subtract 14 days from that.

The formatting has to be:
ex. 2003-02-24
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

check into the PHP date() function
nmotion
Forum Commoner
Posts: 32
Joined: Wed Jan 22, 2003 6:53 am

I looked at it...

Post by nmotion »

But found it rather complex compared to ASP.

In ASP I could just say date-14

So any inputs would be appreciated.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Are you trying to do this to put it into a database? Or have you taken it from a database. (Reason I ask is 'cause databases can generally do this stuff themselves and it saves you a few lines of PHP code.)

Mac
nmotion
Forum Commoner
Posts: 32
Joined: Wed Jan 22, 2003 6:53 am

Post by nmotion »

I need to use it in a sql query.

WHERE $some_date > $current_date-14

Also, I have been trying to figure out how to display the following date:

12-20-2002

As

2002-12-20

with PHP. Yes, I have read about date on php.net but I still can't figure it out. Please help or if you have links to other pages discussing this it would be great.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

MySQL can do the date subtraction stuff, you don't need to fiddle about with PHP for that -

Code: Select all

SELECT field1, field2, field3 FROM table WHERE $some_date > DATE_SUB(NOW(), INTERVAL 14 DAY)
http://www.mysql.com/doc/en/Date_and_ti ... tions.html

As for converting 12-20-2002 to 2002-12-20, try something like:

Code: Select all

$date = '12-20-2002';
$dateinfo = explode('-', $date);
$mysql_date = $dateinfo[2].'-'.$dateinfo[0].'-'.$dateinfo[1];
http://www.php.net/manual/en/function.explode.php

Mac
nmotion
Forum Commoner
Posts: 32
Joined: Wed Jan 22, 2003 6:53 am

Post by nmotion »

Thanks a lot for the help and links.

I got it to work :D
Post Reply