zohab Posted August 24, 2009 Share Posted August 24, 2009 Hi, In my project user enter date in following format and i want to convert it into yyyy-mm-dd format '[b]Y-m-d' => '2006-12-23',[/b] 'm-d-Y' => '12-23-2006', 'd-m-Y' => '23-12-2006', 'Y/m/d' => '2006/12/23', 'm/d/Y' => '12/23/2006', 'd/m/Y' => '23/12/2006', 'Y.m.d' => '2006.12.23', 'd.m.Y' => '23.12.2006', 'm.d.Y' => '12.23.2006', examples input=>24/08/2009 output=>2009-08-24 input=>08.24.2009 output=>2009-08-24 input=>24-08-2009 output=>2009-08-24 any function to perform above task Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/ Share on other sites More sharing options...
5kyy8lu3 Posted August 24, 2009 Share Posted August 24, 2009 i'm sleepy, but this is kinda what you'd need <?php function ConvertDate($OldDate) { $year = substr($OldDate, 6, 4); $month = substr($OldDate, 0, 2); $day = substr($OldDate, 2, 2); $NewDate = $year . '-' . $month . '-' . $day; return($NewDate); } echo ConvertDate('12/23/2006'); //this would output: 2006-12-23 //use a switch statement to choose the input format if you want to be able to convert different formats //but basically you can use substr to pull each number off of the input string Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/#findComment-904971 Share on other sites More sharing options...
PravinS Posted August 24, 2009 Share Posted August 24, 2009 Use this <?php function format_date($date) { $dtarr = explode("/",$date); return $dtarr[2]."-".$dtarr[1]."-".$dtarr[0]; } echo format_date("24/08/2009"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/#findComment-904976 Share on other sites More sharing options...
zohab Posted August 24, 2009 Author Share Posted August 24, 2009 HI guys echo ConvertDate('12/23/2006'); =>2006-12-/2 echo ConvertDate('2009/08/24'); =>8/24-20-09 not getting required result Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/#findComment-904979 Share on other sites More sharing options...
thebadbad Posted August 24, 2009 Share Posted August 24, 2009 The users also specify the format, right? Else it would be impossible to convert. Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/#findComment-904980 Share on other sites More sharing options...
zohab Posted August 24, 2009 Author Share Posted August 24, 2009 Hi User do not specify format we have to detect format and then convert Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/#findComment-904984 Share on other sites More sharing options...
5kyy8lu3 Posted August 24, 2009 Share Posted August 24, 2009 Hi User do not specify format we have to detect format and then convert that would be impossible... how would you tell which is month and which is day for this date: 01/01/2009? if the day is bigger than 12, you know it's the day since months only go to 12, but besides that, there's no way to tell unless the user specifies Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/#findComment-904985 Share on other sites More sharing options...
zohab Posted August 24, 2009 Author Share Posted August 24, 2009 Hi OKay First I will check wheather we can get user entered date format and then convert date to required format Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/#findComment-904997 Share on other sites More sharing options...
PFMaBiSmAd Posted August 24, 2009 Share Posted August 24, 2009 Several of the formats are ambiguous (you can interpret it more than one way) because you cannot tell from the value which is the month and which is the day. You need to limit the format that can be entered by using three drop down select menus, one for the day, one for the month, and one for the year. Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/#findComment-904999 Share on other sites More sharing options...
Catfish Posted August 24, 2009 Share Posted August 24, 2009 or tell the user to input the date in your specific format (YYYY-MM-DD) Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/#findComment-905005 Share on other sites More sharing options...
PFMaBiSmAd Posted August 24, 2009 Share Posted August 24, 2009 You cannot rely on users to read, understand, and follow written instructions. Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/#findComment-905011 Share on other sites More sharing options...
zohab Posted August 24, 2009 Author Share Posted August 24, 2009 OKay I got the format from user examples input=>m-d-Y input=>08-24-2009 required output=>2009-08-24 input=>Y/m/d input=>2009/08/24 required output=>2009-08-24 input=>d.m.Y input=>24-08-2009 required output=>2009-08-24 any solution Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/#findComment-905013 Share on other sites More sharing options...
MadTechie Posted August 24, 2009 Share Posted August 24, 2009 The problem is this given this date 07-03-08, what is the month ? or making it easier 2007-03-08.. but still what's the month ? the only option i can think of is <?php function ConvertDate($OldDate) { return date("Y-m-d",strtotime($OldDate)); } ?> which will return 2007-03-08 from my impossible example. but 23.12.2006 would return 2006-12-23 so you MAY be fine.. Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/#findComment-905024 Share on other sites More sharing options...
thebadbad Posted August 24, 2009 Share Posted August 24, 2009 In that case; <?php function standard_date($input, $format) { $format = preg_replace('~[./-]~', '', $format); $o = preg_split('~[./-]~', $input, 3); switch($format) { case 'Ymd': return implode('-', $o); case 'mdY': return "$o[2]-$o[0]-$o[1]"; case 'dmY': return "$o[2]-$o[1]-$o[0]"; default: trigger_error('Wrong date format supplied to standard_date()', E_USER_ERROR); } } //usage echo standard_date('01.23.2008', 'm.d.Y'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/#findComment-905026 Share on other sites More sharing options...
zohab Posted August 24, 2009 Author Share Posted August 24, 2009 Hi, Following not working echo standard_date('24.08.2009', 'd.m.y'); echo standard_date('24/08/2009', 'd/m/y'); i am getting Fatal error: Wrong date format supplied to standard_date() Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/#findComment-905039 Share on other sites More sharing options...
thebadbad Posted August 24, 2009 Share Posted August 24, 2009 That's because you're using lowercase Ys. Modified to be case insensitive: <?php function standard_date($input, $format) { $format = strtolower(preg_replace('~[./-]~', '', $format)); $o = preg_split('~[./-]~', $input, 3); switch($format) { case 'ymd': return implode('-', $o); case 'mdy': return "$o[2]-$o[0]-$o[1]"; case 'dmy': return "$o[2]-$o[1]-$o[0]"; default: trigger_error('Wrong date format supplied to standard_date()', E_USER_ERROR); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/#findComment-905043 Share on other sites More sharing options...
zohab Posted August 25, 2009 Author Share Posted August 25, 2009 Great work thanks Quote Link to comment https://forums.phpfreaks.com/topic/171618-solved-convert-any-date-to-yyyy-mm-dd-format/#findComment-905708 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.