user5405873
user5405873

Reputation:

how to get time from timestamp using carbon php

I have a problem in extracting time from $timestamp which something looks like

this $timestamp = "2017-05-03 11:47:48";

my question is: how to extract time from above $timestamp like this 11:47 AM

my current time zone is Asia/Kolkata

Upvotes: 3

Views: 5224

Answers (3)

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

First of all this

$timestamp = "2017-05-03 11:47:48";

is not timestamp, Its a date time value in some specific format. To change it to your required format you can try following code in Php:

echo date('H:i A', strtotime($timestamp));

here H is used for 24 Hour format, use h for 12 hours format.

Upvotes: 0

LF-DevJourney
LF-DevJourney

Reputation: 28529

First make sure you installed carbon, refer to https://github.com/briannesbitt/Carbon.

$timestamp = "2017-05-03 11:47:48";
$date = Carbon::createFromFormat('Y-m-d H:i:s', '2017-05-03 11:47:48', 'Asia/Kolkata');
echo $date->format("H:i A");

Upvotes: 2

Sahil Gulati
Sahil Gulati

Reputation: 15141

Hope this will help you out.

Try this code snippet here

<?php
ini_set('display_errors', 1);
$timestamp = "2017-05-03 11:47:48";
$dateObj=date_create_from_format("Y-m-d H:i:s", $timestamp);
echo $dateObj->format("H:i A");

Output: 11:47 AM

Upvotes: -1

Related Questions