0

My intention is to convert the following date

20/04/17 13:27:5 to this

20-04-2017 13:27:05

I tried the typical date format functions of php and also Carbon...

things like

$newDate= Carbon::createFromFormat('d/m/y H:m:s', $originalDate);

in this case var_dump($newDate->toDateTimeString()) would bring 2019-03-20 13:00:55 which is not what I expect.

So I was not lucky....is there a way to do this in a straight forward manner?

4
  • And "was not luck" means what exactly? Commented May 2, 2017 at 9:51
  • Question edited :-) Commented May 2, 2017 at 9:53
  • 1
    That format is hightly orregular, looking at the formats the seconds are specified at. I doubt there is an "automatic" conversion for this, you will have to use string functions to "fix" that detail. Commented May 2, 2017 at 9:57
  • And change H:m:s to H:i:s for minutes Commented May 2, 2017 at 10:00

4 Answers 4

2

I think this should work.

$date = "20/04/17 13:27:5";
$sec  = substr($date, strrpos($date, ":") + 1);
$sec  = substr("0{$sec}", -2);
$new  = substr($date, 0, strrpos($date, ":") + 1) . $sec;

$newDate = Carbon::createFromFormat('d/m/y H:i:s', $new);

I changed the format since you were using m twice for "minutes" and "month". It is correct for the month, but not for the minutes. Instead use i for minutes with leading zeroes.

$sec Is what I used to get the second from the string. This gets the last position of : and will take everything after it. This assumes that you do not change the format of the string.
substr("0{$sec}", -2) Adds a zero to the current second and extracts the last two characters. That means that 50 becomes 050 and then the last two characters are 50 so we end up without the padding, but 5 becomes 05 and the last two characters are the only characters.

$new concatenates the start of the date string and the new second with the zero padding.

$newDate is your original string with the format changed.

Sign up to request clarification or add additional context in comments.

Comments

1

There is issue with seconds. There must be 05 not only 5

<?php

    $original_date = "20/04/17 13:27:5";
    $date_explode = explode(":", $original_date);
    $date_explode[2] = str_pad($date_explode[2],2,"0",STR_PAD_LEFT);
    $original_date = implode($date_explode,":");

    $date = DateTime::createFromFormat('d/m/y H:i:s', $original_date);
    echo date_format($date,"d-m-Y H:i:s");

?>

1 Comment

The seconds are the problem . It is 5 not 05
0

This is a working conversion routine that creates the ISO format you are looking for. But as already mentioned you need to "fix" the strange way the seconds are specified in the original example you provide. You will have to use string functions if that really is the format you receive. Better would be to fix the code that creates such broken formats.

<?php
$input = '20/04/17 13:27:05';
$date = DateTime::createFromFormat('d/m/y H:i:s', $input);
var_dump($date->format('d-m-Y H:i:s'));

The output obviously is:

string(19) "20-04-2017 13:27:05"

Comments

0

Isn't it like this?

$newDate = Carbon::createFromFormat('d/m/y H:i:s', $originalDate);

1 Comment

Fatal error: Uncaught InvalidArgumentException: A two digit second could not be found. This is the error that is returned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.