php 字符串日期当前日期/时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5180730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:34:10  来源:igfitidea点击:

String date current date/time?

phpdatetime

提问by Cam

I am using $date = date("D M d, Y G:i");.

我正在使用 $date = date("D M d, Y G:i");.

When I echo $date, it shows the correct date/time. Now I need this as an string.

当我 echo 时$date,它显示正确的日期/时间。现在我需要它作为一个字符串。

I have tried string($date); but nothing happens here. And

我试过了string($date);但这里什么也没发生。和

$today = strtotime($date); 

here I get weird numbers..

在这里,我得到了奇怪的数字..

I need a string so I can put $todayin a message.

我需要一个字符串,以便我可以$today输入消息。

What is the correct method for this?

什么是正确的方法?

回答by Pascal MARTIN

The date()function already returns a string.

date()函数已经返回一个字符串。

Doing this :

这样做:

$date = date("D M d, Y G:i");

You'll have the current date in the $datevariable, as a string -- no need for any additional operation.

您将在$date变量中获得当前日期,作为一个字符串——不需要任何额外的操作

回答by alakin_11

If you like working with objects you can do this:

如果你喜欢使用对象,你可以这样做:

$date = new \DateTime('now');
echo $date->format('D M d, Y G:i');

回答by krtek

Your $datevariable is a string, there's no need for any conversion.

你的$date变量是一个字符串,不需要任何转换。

You can have a look at the documentation: http://ch.php.net/manual/en/function.date.php. The return value of the date()function is string.

您可以查看文档:http: //ch.php.net/manual/en/function.date.phpdate()函数的返回值是字符串。

The strange numbers you see when you call strtotime()is the Unix timestamp which represents the number of seconds elapsed since January 1 1970 00:00:00 UTC.

您在调用时看到的奇怪数字strtotime()是 Unix 时间戳,它表示自 1970 年 1 月 1 日 00:00:00 UTC 以来经过的秒数。

回答by JohnP

You're already getting a string. $date can be used like any string now.

你已经得到了一个字符串。$date 现在可以像任何字符串一样使用。

strtotime()actually gives you the number of seconds in time like unix

strtotime()实际上给你像unix一样的时间秒数

回答by prof.Bruce

$date = 'Today is '.date("D M d, Y G:i", time());
echo $date;

回答by Alex

With regards to:

关于:

$today = strtotime($date); 

Those numbers are the current timestamp (the number of seconds since January 1st 1970). You can use this as a second parameter in the date function to change the date to whatever you want.

这些数字是当前时间戳(自 1970 年 1 月 1 日以来的秒数)。您可以将其用作日期函数中的第二个参数,以将日期更改为您想要的任何值。

$newDate = date("D M d, Y G:i", $timeStamp);

回答by Alex

$today = mktime(0,0,0,date("m"),date("d"),date("Y"));

Then:

然后:

echo "Today is ".date("Y/m/d", $today);

According to: http://www.w3schools.com/php/php_date.asp

根据:http: //www.w3schools.com/php/php_date.asp