jmahdi Posted November 17, 2011 Share Posted November 17, 2011 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 More sharing options...
jmahdi Posted November 17, 2011 Author Share Posted November 17, 2011 when i try to enter it into mysql database it comes as 0000-00-00 00:00:00 ... thats another issue... sorry but i'm alien to date formatting Link to comment https://forums.phpfreaks.com/topic/251306-converting-to-a-date-format/#findComment-1288919 Share on other sites More sharing options...
Pikachu2000 Posted November 17, 2011 Share Posted November 17, 2011 How is the user inputting the date in the form? Link to comment https://forums.phpfreaks.com/topic/251306-converting-to-a-date-format/#findComment-1288922 Share on other sites More sharing options...
ManiacDan Posted November 17, 2011 Share Posted November 17, 2011 $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 Link to comment https://forums.phpfreaks.com/topic/251306-converting-to-a-date-format/#findComment-1288950 Share on other sites More sharing options...
jmahdi Posted November 17, 2011 Author Share Posted November 17, 2011 How is the user inputting the date in the form? sorry i couldn't get what you mean...do you mean if the user is shown a certain format to stick to when entering the date? if you meant so, i didn't do so... Link to comment https://forums.phpfreaks.com/topic/251306-converting-to-a-date-format/#findComment-1288978 Share on other sites More sharing options...
ManiacDan Posted November 17, 2011 Share Posted November 17, 2011 You didn't reply to my message, which shows you exactly why you need to enforce a certain format from the users. Link to comment https://forums.phpfreaks.com/topic/251306-converting-to-a-date-format/#findComment-1288984 Share on other sites More sharing options...
jmahdi Posted November 17, 2011 Author Share Posted November 17, 2011 You didn't reply to my message, which shows you exactly why you need to enforce a certain format from the users. thanks for your reply and sorry for me not replying, I was just about look into applying what you had told me....I'll be back Link to comment https://forums.phpfreaks.com/topic/251306-converting-to-a-date-format/#findComment-1289050 Share on other sites More sharing options...
jmahdi Posted November 18, 2011 Author Share Posted November 18, 2011 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 Link to comment https://forums.phpfreaks.com/topic/251306-converting-to-a-date-format/#findComment-1289375 Share on other sites More sharing options...
ManiacDan Posted November 18, 2011 Share Posted November 18, 2011 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. Link to comment https://forums.phpfreaks.com/topic/251306-converting-to-a-date-format/#findComment-1289376 Share on other sites More sharing options...
jmahdi Posted November 18, 2011 Author Share Posted November 18, 2011 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.... Link to comment https://forums.phpfreaks.com/topic/251306-converting-to-a-date-format/#findComment-1289380 Share on other sites More sharing options...
litebearer Posted November 18, 2011 Share Posted November 18, 2011 how on earth can i guess answer: you can't; however, you can design your page to solve the issue. just a couple of ways 1) drop downs for day, month, year, 2) calendars Link to comment https://forums.phpfreaks.com/topic/251306-converting-to-a-date-format/#findComment-1289389 Share on other sites More sharing options...
ManiacDan Posted November 18, 2011 Share Posted November 18, 2011 Exactly. Link to comment https://forums.phpfreaks.com/topic/251306-converting-to-a-date-format/#findComment-1289391 Share on other sites More sharing options...
jmahdi Posted November 18, 2011 Author Share Posted November 18, 2011 how on earth can i guess answer: you can't; however, you can design your page to solve the issue. just a couple of ways 1) drop downs for day, month, year, 2) calendars thanks litebearer.... Link to comment https://forums.phpfreaks.com/topic/251306-converting-to-a-date-format/#findComment-1289421 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.