将三个可变数据转换为php中的日期格式

问题描述 投票:2回答:5

我有三个变量包含如下所示的值

                       $day=13
                       $month=2
                       $year=2013

我想将这三个变量数据转换为php中的日期格式并存储在另一个变量Ho中来执行此操作?

php date date-conversion
5个回答
4
投票

另一种处理方法是使用DateTime

$date = new DateTime($year.'-'.$month.'-'.$day);
echo $date->format('Y-m-d');

4
投票

PHP实际上没有日期变量类型,但它可以处理时间戳;可以使用mktime()创建这样的时间戳:

$ts = mktime(0, 0, 0, $month, $day, $year);

然后可以与date()一起使用来格式化它:

$formatted = date('Y-m-d', $ts);

1
投票

下面的代码将为您解决问题。将日期格式传递给date()函数。

echo date("M-d-Y", mktime(0, 0, 0, $month, $day, $year));

O / P:2013年2月13日


0
投票

使用strtotime:http://php.net/manual/en/function.strtotime.php

$date_timestamp = strtotime($month . " " . $day . ", " . $year);

这将返回一个时间戳,您可以使用该时间戳来创建日期:

$the_date = date("M d, Y", $date_timestamp);

但是,如果您只想要一串这些日期:

$the_date = $month . " " . $day . ", " . $year;

0
投票

您可以使用mktime()函数

$ts = mktime(0, 0, 0,$month, $day, $year);

然后将$ ts传递给日期函数

$formatted = date('Y-m', $ts);
© www.soinside.com 2019 - 2024. All rights reserved.