Jump to content

Recommended Posts

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

 

 

 

 

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

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

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.

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

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..

 

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');
?>

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);
}
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.