0

I have a problem in my application. I have to convert arriving date string to server date format. So I planned to sent date along with the date format to the model class. Like

$requiredFormat='Y-m-d';
Function($givenDate,$givenFormat){
//convert given date to yyyy-mm-dd format
return $yyyy-mm-dd;
}

I have different types of client for my model, My web client use one format, and my mobile clients use another format, So I decide sent date format along with the date.

I cant use strtotime function because the format may differ in different calls. The required function will get 2 arguments (arrivedDate,arrivedDateFormat) And using these 2 argument , convert the arrivedDate to YYYY-mm-dd format So Please provide me the best way to do this.

Sridhar
  • 2,228
  • 10
  • 48
  • 79
  • where's `$givenFormat` used and why is it needed when you want the format `yyyy-mm-dd` after all? – tradyblix Oct 29 '12 at 05:50
  • different client may access my model, so I cant exactly know correct format of the imcoming dates – Sridhar Oct 29 '12 at 05:51
  • My server date format is yyyy-mm-dd – Sridhar Oct 29 '12 at 05:52
  • Force a check on the incoming date from the application, return an error if it's in the wrong format. You're overcomplicating the problem. I have never seen a date input field that didn't specify how it wanted the date formatted. – Rick Calder Oct 29 '12 at 05:54
  • convert the incoming date into unix timestamp with strtotime and then return it with date('format', $incomingtime) – Svetoslav Oct 29 '12 at 05:54
  • possible duplicate of [PHP convert date format dd/mm/yyyy => yyyy-mm-dd](http://stackoverflow.com/questions/10306999/php-convert-date-format-dd-mm-yyyy-yyyy-mm-dd) – vascowhite Oct 29 '12 at 06:12
  • You can try my solution. You just have to call a function and pass two parameters. The date and the format. – Abhishek Saha Oct 29 '12 at 07:11
  • @Abhishek Saha, Yes I have to call a function that takes 2 argument such as arrivedDate and arrivedDateFormat, Using these 2 argument convert arrivedDate to Y-m-d format – Sridhar Oct 29 '12 at 07:28
  • So whats the problem now ? You can pass the format as a parameter to the function like getDate('Y-m-d',$time); – Abhishek Saha Oct 29 '12 at 07:33
  • my function should be capable of handle any date format, ie My function will get any formatted date and convert it to Y-m-d – Sridhar Oct 29 '12 at 07:39

4 Answers4

0

I think you should use strtotime.

Something like:

echo date('YY "-" mm "-" dd', strtotime($givenDate));

And also it will be good if you show us how $givenDate looks like.

loler
  • 2,594
  • 1
  • 20
  • 30
0

Use this

Function mydate($givenDate,$givenFormat){
return date($givenFormat,strtotime($givenDate));
}

Use your format as required like "Y-m-D" or "yyyy-mm-dd"

and call functin like

 echo mydate("29-oct-2012","Y-m-d");

SEE LIVE DEMO

StaticVariable
  • 5,253
  • 4
  • 23
  • 45
  • your function will return date in givenformat, But i need date in server format – Sridhar Oct 29 '12 at 06:12
  • echo 'Given format:mon-day-year is 8-7-2012='.mydate('8-7-2012', "Y-m-d").'Transtormed to: year-mon-day
    '; echo 'Given format:mon-day-year is 12-8-2012='.mydate('12-8-2012', "Y-m-d").'Transtormed to: year-mon-day';.
    – Sridhar Oct 29 '12 at 06:25
  • Given format:mon-day-year is 8-7-2012=2012-07-08Transtormed to: year-mon-day Given format:mon-day-year is 12-8-2012=2012-08-12Transtormed to: year-mon-day – Sridhar Oct 29 '12 at 06:26
  • @Sridhar so what is problem ? you want to convert `mon-day-year` to `year-mon-day`? – StaticVariable Oct 29 '12 at 06:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18700/discussion-between-sridhar-and-jhonraymos) – Sridhar Oct 29 '12 at 07:04
0

Finally I got my required function

    function convertDate($givenDate,$givenFormat)
{
    $date = DateTime::createFromFormat($givenFormat, $givenDate);
    $ret= $date->format('Y-m-d');
    return $ret;
}
Sridhar
  • 2,228
  • 10
  • 48
  • 79
-3

Write the function like this. Edit: Corrected from comments

function getDate($givenDate,$givenFormat){
//convert given date to yyyy-mm-dd format
$newdate = date($givenFormat, strtotime($givenDate));
return $newdate;
}
Abhishek Saha
  • 2,564
  • 1
  • 19
  • 29
  • $givenDate='05/25/2012'; $datefrmt='m/d/y'; $newdate = date('Y-m-d', strtotime('m/d/Y',$givenDate)); echo $newdate; – Sridhar Oct 29 '12 at 06:03
  • strtotime('m/d/Y',$givenDate) return nothing – Sridhar Oct 29 '12 at 06:05
  • @Sridhar: Of coure you'll get `1970-01-01` because Abhishek Saha used `strtotime` in a wrong manner. it should be `$newdate = date('Y-m-d', strtotime($givenDate)); ` – sephoy08 Oct 29 '12 at 06:07