如何从PHP中的日期获取数字?

时间:2016-08-29 13:11:37

标签: php mysql date

我将日期以varchar格式存储在数据库中。我想从输入日期获得8位数字。输入日期可以是任何格式,如2016年8月28日,28-08-2016或2016年8月28日等。

现在我试着这样做:

    $country = mb_substr($this->country, 0, 4);
    $username = $this->user_name;
    $converted_date = date('Y_m_d-H-i-s', strtotime($this->date_of_application));
    $this-> member_id = $country.$username.$converted_date;

我正在连接字符串和日期。所以我的输入日期是: 2016年8月28日和我得到的输出是:1970_01_01-04-00-00

我在php.ini文件中更改了时区。

所以我希望输出为28082016.我怎么能得到这个?

4 个答案:

答案 0 :(得分:1)

'Y_m_d-H-i-s'是日期的格式。因此,如果您想将日期连接到28082016,那么应该这样做:

$converted_date = date('dmY', strtotime($this->date_of_application));

答案 1 :(得分:0)

您应该使用DateTime Class

$input = '28-08-2016';
$input = str_replace("/", "-", $input);
$date = new DateTime($input);
echo $date->format('dmY'); // 28082016

$input = '28 Aug 2016';
$input = str_replace("/", "-", $input);
$date = new DateTime($input);
echo $date->format('dmY');  // 28082016

$input = '28/08/2016';
$input = str_replace("/", "-", $input);
$date = new DateTime($input);
echo $date->format('dmY');  // 28082016

答案 2 :(得分:0)

使用日期('dmY',strtotime(您在字符串中的日期));

答案 3 :(得分:0)

28/08/2016日,月和四位数年日期格式的错误表示法。
每个部分应以点,制表符或短划线分隔在这种情况下:

$d = "28/08/2016";
$converted_date = date('Y_m_d-H-i-s', strtotime(str_replace("/", ".", $d)));

print_r($converted_date);  // "2016_08_28-00-00-00"

http://php.net/manual/en/datetime.formats.date.php