如何从完整的“日期"字符串中仅提取“天"值? [英] How to extract only 'Day' value from full 'Date' string?
问题描述
我有一个格式为'Y-m-d'
的Date值数组.我想遍历数组,只提取每个成员的日期('d'
),但不知道要拆分它.我会以某种方式使用子字符串吗?
I have an array of Date values in the format of 'Y-m-d'
. I want to loop over the array and only extract the day ('d'
) of each member, but can't figure out to split it. Do I use substrings in some way?
简单地说,我有'2010-11-24'
,我想从中得到 '24'
.问题是一天可能是个位数还是个位数.
Put simply, I have '2010-11-24'
, and I want to get just '24'
from that. The problem being the day could be either single or double figures.
推荐答案
如果您有PHP<5.3,使用 strtotime()
:
If you have PHP < 5.3, use strtotime()
:
$string = "2010-11-24";
$timestamp = strtotime($string);
echo date("d", $timestamp);
如果您的PHP> = 5.3,请使用基于 DateTime
的解决方案,并使用 createFromFormat - DateTime
是将来的日期,可以处理1900年和2038年以后的日期:
If you have PHP >= 5.3, use a DateTime
based solution using createFromFormat - DateTime
is the future and can deal with dates beyond 1900 and 2038:
$string = "2010-11-24";
$date = DateTime::createFromFormat("Y-m-d", $string);
echo $date->format("d");
这篇关于如何从完整的“日期"字符串中仅提取“天"值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!