简体   繁体   English

如何使用Carbon php从时间戳获取时间

[英]how to get time from timestamp using carbon php

I have a problem in extracting time from $timestamp which something looks like 我在从$timestamp中提取时间时遇到了问题

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

my question is: how to extract time from above $timestamp like this 11:47 AM 我的问题是:如何从$timestamp上面提取时间,像这样11:47 AM

my current time zone is Asia/Kolkata 我当前的时区是Asia/Kolkata

First make sure you installed carbon, refer to https://github.com/briannesbitt/Carbon . 首先确保您安装了Carbon,请参阅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");

First of all this 首先

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

is not timestamp , Its a date time value in some specific format. 不是timestamp ,它是某种特定格式的日期时间值。 To change it to your required format you can try following code in Php: 要将其更改为所需的格式,可以尝试在Php中执行以下代码:

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

here H is used for 24 Hour format, use h for 12 hours format. 这里的H表示24小时制,使用h表示12小时制。

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 输出: 11:47 AM 11:47 AM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM