简体   繁体   English

如何将日期时间分成不同的部分?

[英]How can i split datetime into different parts?

I have a column in my database named created_at (I am using laravel 5.2).Its value is something like this: 2016-10-01 01:22:13 .I want to split this date time in Year,Month,Day,Hour,Minute,Second. 我的数据库中有一列created_at (我正在使用laravel 5.2)。它的值是这样的: 2016-10-01 01:22:13 。我想将此日期时间拆分为Year,Month,Day,Hour ,分钟,第二。

I can go with below approach for just date have 2016-10-01 : 我可以使用以下方法来获得日期2016-10-01

$resDate= explode('-', $resDate);
$year  = $resDate[0];
$month = $resDate[1];
$day   = $resDate[2];

But if date and time is altogether ( 2016-10-01 01:22:13 ) then how can i split it? 但是如果日期和时间2016-10-01 01:22:132016-10-01 01:22:13 )那么我该如何拆分呢?

Use date_parse_from_format : Get info about given date formatted according to the specified format. 使用date_parse_from_format :获取有关根据指定格式进行格式化的给定日期的信息。

$date = '2016-10-01 01:22:13';
$split = date_parse_from_format('Y-m-d h:i:s', $date);

var_dump($split['year']); 
// outputs year from the string, 
//Similarly `$split['month']` will produce month name and so on.

Solution: 解:

As per the data that you have in the DB you can concert the date to strtotime() and then format it as you required. 根据数据库中的数据,您可以将日期与strtotime() ,然后根据需要设置其格式。

Example: 例:

$date = 2016-10-01 01:22:13

then you may perform the following actions 那么您可以执行以下操作

echo  $date_alone = date('d',strtotime($date)) // 01

Like wise you can fetch all the data that you have based on the codes given below 同样,您可以根据下面给出的代码获取所有数据

d - Date d日期

m - month m

Y - Year Y

H - Hour H小时

i - Minutes i -分钟

s - Seconds s

Below you can follow it for the reference purpose. 您可以在下面按照以下说明进行参考。

Date and time formatting with PHP 用PHP格式化日期和时间

2008-02-07 16:45:58 (Consider it as Example) 2008-02-07 16:45:58(以示例为例)

For those of you that are not genius enjoying themselves by calculating big integer time, I will reveal the mystery of how we can format dates and time. 对于那些不是天才通过计算大整数时间来享受乐趣的人,我将揭示如何设置日期和时间格式的奥秘。 We will begin with dates formatting. 我们将从日期格式开始。 It is done using date() function. 它使用date()函数完成。 It accepts two parameters. 它接受两个参数。 Format string and time as integer. 将字符串和时间格式化为整数。 We will use time() for current time integer. 我们将使用time()作为当前时间整数。 Let's see some examples. 让我们看一些例子。

echo date("d-m-y", time());     // 07-02-08

The dash between the characters in the format string above is exactly what the output is going to be separated with. 上面的格式字符串中的字符之间的破折号恰好是将与输出分隔的内容。

echo date("D j/n/Y", time());     // Thu 7/2/2008

The dash here is replaced with forward slash. 此处的破折号被正斜杠代替。

echo date("jS of F Y", time());     // 7th of February 2008

We can use any character we want here, just keep in mind that if it has a special meaning it has to be escaped. 我们可以在此处使用我们想要的任何字符,只需记住,如果它具有特殊含义,则必须将其转义。 And just to be sure that there are no mistakes I suggest that you always escape every character you use. 并且为了确保没有错误,我建议您始终避免使用的每个字符。

echo date("d M y", time());     //07 Feb 08

Formatting the date using short representation for month names and short year. 使用简短表示法表示日期和月份格式来格式化日期。

echo date("l jS of F", time());     // Thursday 7th of February

Full day and month name with ordinal suffix for the day of the month. 全月和全月名称,带月序号后缀。

Date format characters' legend by examples: 日期格式字符的图例示例:

d - Numeric representation of a day, with leading zeros 01 through 31. d-一天的数字表示形式,前导零从01到31。

m - Numeric representation of a month, with leading zeros 01 through 12. m-一个月的数字表示形式,前导零从01到12。

y - Numeric representation of a year, two digits. y-一年的数字表示形式,两位数。

D - Textual representation of a day, three letters Mon through Sun. D-一天的文本表示形式,星期一至星期日三个字母。

j - Numeric representation of a day, without leading zeros 1 through 31. j-一天的数字表示形式,前导零从1到31。

n - Numeric representation of a month, without leading zeros 1 through 12. n-一个月的数字表示形式,前导零从1到12。

Y - Numeric representation of a year, four digits. Y-一年的数字表示形式,四位数。

S - English ordinal suffix for the day of the month. S-每月的英语序数后缀。 Consist of 2 characters st, nd, rd or th. 由2个字符st,nd,rd或th组成。

F - Textual representation of a month, January through December. F-一月到十二月的月份的文本表示形式。

M - Textual representation of a month, three letters Jan through Dec. M-一个月的文本表示形式,1月至12月为三个字母。

l textual representation of the day of the week Sunday through Saturday. l星期日至星期六的星期几的文本表示形式。 Now we move on to our next task which is time formatting. 现在,我们进入下一个任务,即时间格式化。 We are still going to use the same date() function, but with different format characters. 我们仍将使用相同的date()函数,但使用不同的格式字符。

echo date("G:i:s", time());     //16:45:58

Working with time is a little bit simpler, because there is only one way to represent minutes and seconds. 时间的处理稍微简单一点,因为只有一种表示分钟和秒的方法。

echo date("H:i:s", time());    //16:45:58

Despite of the very few options for minutes and seconds formats we have a lot of hour formatting styles. 尽管分钟和秒格式的选择很少,但我们还是有许多小时格式样式。 This one represents hours with leading zeroes 0 through 23. 这个代表小时,前导0到23。

echo date("g:i a.", time());     //4:45 pm.

For all i know there isn't a function that fills your datetime value parts into an array in php. 就我所知,没有一个函数可以将您的datetime值部分填充到php中的数组中。 You can create your own function to split your datetime value into an array. 您可以创建自己的函数,以将日期时间值拆分为一个数组。

Example: 例:

function dateTimeToArray($myDateTime)
{
    $dateTime = new DateTime($myDateTime);

    $date = $datetime->format('Y-m-d');
    $time = $datetime->format('H:i:s');

    $dateTimeArray = array_merge(explode('-', $date), explode(':', $time));

    return $dateTimeArray;
}

In Laravel models you have datetime fields as Carbon object. 在Laravel模型中,您将日期时间字段作为Carbon对象。

So you can simply call ->toDateString() if a date is needed 因此,如果需要日期,您可以简单地调用-> toDateString()

$modelObject->created_at->toDateString();

http://carbon.nesbot.com/docs/#api-formatting http://carbon.nesbot.com/docs/#api-formatting

Also you can access directly to all values of the date. 您也可以直接访问日期的所有值。 http://carbon.nesbot.com/docs/#api-getters http://carbon.nesbot.com/docs/#api-getters

$year  = $modelObject->created_at->year;
$month = $modelObject->created_at->month;
$day   = $modelObject->created_at->day;

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

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