从时间戳获取日期将返回月份

时间:2016-05-11 07:17:16

标签: php date

我有以下日期时间:

09/05/2016 09:12 (9 May 2016)

我想从中获取这一天所以

date('d', strtotime($date))

但这会返回05,即月份。

我做错了什么?

4 个答案:

答案 0 :(得分:4)

这是因为{J {认为这是美国的日期格式。

尝试将斜杠(/)更改为短划线(-)

// that is recognized as day month year, not month/day/year
09-05-2016

如果您无法以其他格式获取日期,则可以使用str_replace删除斜杠

$date = str_replace('/', '-', $date);

这里有link可以帮到你。

答案 1 :(得分:3)

使用DateTime类将为您提供更多灵活性。例如:查看createFromFormat方法,该方法可以将任何格式的日期和时间转换为有效的DateTime对象。

// Define your date
$date = '09/05/2016 09:12';

// convert it to a DateTime object
$date = DateTime::createFromFormat('d/m/Y H:i', $date);

// Output - returns 09
echo $date->format('d');

有关更多信息,请查看日期时间上的PHP Documentation

答案 2 :(得分:0)

首先按 UIButton *frontButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [frontButton setImage:[[UIImage imageNamed:FRONT_IMAGE] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal]; [frontButton.imageView setTintColor:[UIColor redColor]]; [self addSubview:frontButton]; 设置格式的日期时间。然后打电话给你的一天!

date()

答案 3 :(得分:0)

如您所知,您的日期格式不正确,请按照您的需要重新格式化。只需根据需要创建日期。

  

date_create将从您的字符串中创建一个新日期。 date_format

     

会将您的日期转换为新格式最后将date函数转换为

     

strtotime会显示您想要的结果。

$str = "09/05/2016 09:12";

$date = date_create($str);
$time = date_format($date, "m-d-Y h:i");

echo date("d", strtotime($time)); //09