Cam
Cam

Reputation: 1959

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?

Upvotes: 23

Views: 99670

Answers (6)

alakin_11
alakin_11

Reputation: 719

If you like working with objects you can do this:

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

Upvotes: 4

Alex
Alex

Reputation: 7374

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);

Upvotes: 0

krtek
krtek

Reputation: 26597

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.

Upvotes: 2

prof.Bruce
prof.Bruce

Reputation: 34

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

Upvotes: 1

JohnP
JohnP

Reputation: 50009

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

Upvotes: 1

Pascal MARTIN
Pascal MARTIN

Reputation: 400932

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.

Upvotes: 71

Related Questions