user7398637
user7398637

Reputation: 11

How to convert Datetime value to date time

I have a datetime value 20170109221930 given by my client.I can't understand how to convert this value to datetime. Also I can't understand which format (20170109221930) my client give me?

Upvotes: 0

Views: 1395

Answers (3)

Gökhan Peker
Gökhan Peker

Reputation: 1

You can use Carbon method in Laravel like this:

{{ Carbon\Carbon::parse($your_time)->format('d.m.Y') }}

in your view file.

You can find more info

https://laracasts.com/discuss/channels/general-discussion/use-carbon-at-blade-template?page=1

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163788

Since you're using Laravel you can use Carbon's parse() or createFromFormat() methods:

$dateObject = Carbon::parse('20170109221930');

After that you'll be able to use tens Carbon methods to convert this timestamp to whatever format you want:

$dateObject->format(h:i:s);
$dateObject->toDateTimeString();

Upvotes: 1

LF-DevJourney
LF-DevJourney

Reputation: 28529

use createFromFormat to create datetime from a string. then you can use format to format it.

$string = '20170109221930';
var_dump(DateTime::createFromFormat('YmdHis', $string));

output:

ei@localhost:~$ php test.php
object(DateTime)#1 (3) {
  ["date"]=>
  string(19) "2017-01-09 22:19:30"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(14) "Asia/Chongqing"
}

Upvotes: 2

Related Questions