296

I have already researched a lot of site on how can I convert PHP DateTime object to String. I always see "String to DateTime" and not "DateTime to String"

PHP DateTime can be echoed, but what i want to process my DateTime with PHP string functions.

My question is how can I make PHP dateTime Object to a string starting from this kind of code:

$dts = new DateTime(); //this returns the current date time
echo strlen($dts);
2
  • 2
    Why do you want to process your DateTime with string functions? What are you trying to accomplish? Commented May 13, 2012 at 3:18
  • 2
    some kind of a datetime validator? in php Commented May 13, 2012 at 3:22

6 Answers 6

554

You can use the format method of the DateTime class:

$date = new DateTime('2000-01-01');
$result = $date->format('Y-m-d H:i:s');

If format fails for some reason, it will return FALSE. In some applications, it might make sense to handle the failing case:

if ($result) {
  echo $result;
} else { // format failed
  echo "Unknown Time";
}
Sign up to request clarification or add additional context in comments.

5 Comments

+1 It works. Can you please also tell how to check for null (in same line) before doing this conversion. Thanks!
Hey @Watt, good point! I've expanded the example to show how the failing case might be handled.
you forgot the timezone, instead use $date->format('c') or $date->format('Y-m-d H:i:s Z') or $date->format('Y-m-d H:i:s T')
what using statement do you need to be able to use ->format? It says missing something in Zend
You can use this shorter form $result = (new DateTime('2000-01-01'))->format('Y-m-d H:i:s'); since PHP 5.4. Watch here: docs.php.net/manual/en/migration54.new-features.php Class member access on instantiation has been added, e.g. (new Foo)->bar().
26

The simplest way I found is:

$date   = new DateTime(); //this returns the current date time
$result = $date->format('Y-m-d-H-i-s');
echo $result . "<br>";
$krr    = explode('-', $result);
$result = implode("", $krr);
echo $result;

I hope it helps.

Comments

25
echo date_format($date,"Y/m/d H:i:s");

Comments

20

There are some predefined formats in date_d.php to use with format like:

define ('DATE_ATOM', "Y-m-d\TH:i:sP");
define ('DATE_COOKIE', "l, d-M-y H:i:s T");
define ('DATE_ISO8601', "Y-m-d\TH:i:sO");
define ('DATE_RFC822', "D, d M y H:i:s O");
define ('DATE_RFC850', "l, d-M-y H:i:s T");
define ('DATE_RFC1036', "D, d M y H:i:s O");
define ('DATE_RFC1123', "D, d M Y H:i:s O");
define ('DATE_RFC2822', "D, d M Y H:i:s O");
define ('DATE_RFC3339', "Y-m-d\TH:i:sP");
define ('DATE_RSS', "D, d M Y H:i:s O");
define ('DATE_W3C', "Y-m-d\TH:i:sP");

Use like this:

$date = new \DateTime();
$string = $date->format(DATE_RFC2822);

1 Comment

You can use constant of DateTime (eg DateTime::ISO8601), it makes the code easier to read.
2

Shorter way using list. And you can do what you want with each date component.

list($day,$month,$year,$hour,$min,$sec) = explode("/",date('d/m/Y/h/i/s'));
echo $month.'/'.$day.'/'.$year.' '.$hour.':'.$min.':'.$sec;

Comments

1

This worked for me

$start_time   = date_create_from_format('Y-m-d H:i:s', $start_time);
$current_date = new DateTime();
$diff         = $start_time->diff($current_date);
$aa           = (string)$diff->format('%R%a');
echo gettype($aa);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.