开发者

String date current date/time?

I am using $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.

I have tried string($date); but nothing 开发者_开发知识库happens here. And

$today = strtotime($date); 

here I get weird numbers..

I need a string so I can put $today in a message.

What is the correct method for this?


The date() function already returns a string.

Doing this :

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

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


If you like working with objects you can do this:

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


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

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.

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.


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

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


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


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.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜