Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String date current date/time?

Tags:

php

datetime

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?

like image 342
Cam Avatar asked Mar 03 '11 12:03

Cam


People also ask

How can I get current date in PHP string?

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.

How can I get current date and time in PHP?

Answer: Use the PHP date() Function You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.


2 Answers

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.

like image 85
Pascal MARTIN Avatar answered Oct 16 '22 08:10

Pascal MARTIN


If you like working with objects you can do this:

$date = new \DateTime('now');
echo $date->format('D M d, Y G:i');
like image 22
alakin_11 Avatar answered Oct 16 '22 08:10

alakin_11