0

i need convert my returned time date " 2018-09-28T16:00:05.000Z " like this

2018-09-28 12:05 AM

use php and javascript , i want use one of them or two language together

3 Answers3

0

Use strtotime() to interpret any standard format of time to timestamp. Then use date() or strftime() for your output.

echo date('Y-m-d h:i A', strtotime($youtube_time));
tim
  • 2,530
  • 3
  • 26
  • 45
0

You should be able to parse this in javascript outright, but formatting the output is a pain, so I'd suggest using a library like momentjs. The format is not compatible with php's but is much better than nothing. Since this happens on the client, it will display the user's localtime.

var s = "2018-09-28T16:00:05.000Z";

var d = new Date(s);
document.getElementById("js").innerHTML = d;

var m = moment(s);
document.getElementById("m").innerHTML = m.format("YYYY-MM-DD h:MM A");
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<div id="js"></div>
<div id="m"></div>

As for php, it's ISO8601 but not really, so you need to format the time and convert it to the appropiate timezone if necessary:

$d = DateTime::createFromFormat("Y-m-d\TH:i:s.uO", "2018-09-28T16:00:05.000Z");

var_dump($d->format("Y-m-d h:i A"));
msg
  • 7,863
  • 3
  • 14
  • 33
-1

The logic is quite linear, so I think the function will speak for itself. This is in javascript, it can be converted to PHP but the code will be mostly the same.

function(datetime) {
    date = datetime.split("T")[0];
    time = datetime.split("T")[1];
    hour = time.split(":")[0]
    minute = time.split(":")[1]
    halfday = "AM"
    if (hour > 12) {
        hour = hour - 12
        halfday = "PM"
    } else if(hour == 12) {
        halfday = "PM"
    }
    return date + " " + hour + ":" + minute + " " + halfday
}
Nicholas Pipitone
  • 4,002
  • 4
  • 24
  • 39