date formating variable

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
hmsg
Forum Commoner
Posts: 42
Joined: Sun May 14, 2006 9:48 am

date formating variable

Post by hmsg »

Hi.

I have a variable $d that contain a date but in this format ex.: 2006-5-1 , but now i need to make a select to a mysql DB the problem is that in mysql the date type is 2006-05-01, how can i solve this. Is it possible use the function date() of php like this: date("Ymd", $d);

Thank for anyone help


HMSG
timclaason
Forum Commoner
Posts: 77
Joined: Tue Dec 16, 2003 9:06 am
Location: WI

Date

Post by timclaason »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


You could do an explode

Code: Select all

$newdate = explode("-", $d);
$newyear = $newdate[0];

for($index = 1; $index < 3; $index++) {
if($newdate[$index] < 10) {
$newdate[$index] = "0" . $newdate[$index];
}
}
$formattednewdate = $newdate[0] . "-" . $newdate[1] . "-" . $newdate[2];

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Code: Select all

date("Y-m-d", strtotime("2006-5-1"));
//output: 2006-05-01
Post Reply