Jump to content

Converting to a date format


jmahdi

Recommended Posts

Hi there,

 

i want to convert whatever a user inputs through and html form to a DATE format which when posted will be entered (using mysql) as that particular Date format , i'm using this:

 

$date = "12-12-11"; //assume this to be the user input

	$time = strtotime($date);
	$new_date = date("d/m/Y",$time);
                echo $new_date;

 

but this format will give the output as:  11/12/2012.....is there a way to convert whatever the user enters to be: dd/mm/yyyy which gives: 12/12/2011

thanks so much

 

Link to comment
https://forums.phpfreaks.com/topic/251306-converting-to-a-date-format/
Share on other sites

$date = "10-12-11"; //assume this to be the user input

What date is that?  Is that October 12, 2011?  Is it December 10, 2011?  Is it December 11, 2010?  All three of those dates are valid interpretations of that string.  That's your problem: Your user input is ambiguous.

 

You need to attempt to use explode() and mktime() to make a valid timestamp out of these dates, then you can use date() to format it however you like.

 

-Dan

You didn't reply to my message, which shows you exactly why you need to enforce a certain format from the users.

 

how about this for a converter:

$signs = array("-", "\\", "/", "*");
	$rpl = str_replace($signs, ' ', $date);
	$d = explode(" ", $rpl);
	$date = mktime(0,0,0,$d[0], $d[1], $d[2]);
	echo $date;
	echo strftime("%d/%m/%Y", $date);

I think this solves it....thanks again for the tip

That assumes you know what order the user is entering the date in.  If you're 100% confident that they'll always use that order (and they're not, say, european or a smartass like me) then you're fine.

 

in fact...i give up :( ... how on earth can i guess in what order the user is inputting the date? help please....

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.