Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new DateTime() vs new DateTime('NOW') in php

Tags:

php

datetime

Before I post this question, I searched in several places for answer for this. I really could not find the proper answer.

I evaluated the both way following...

$date = new DateTime()

amd

$date = new DateTime('NOW')

Is it must for the both above give the same result or is there any circumstance they both differ?

like image 594
fSazy Avatar asked Jan 28 '14 06:01

fSazy


People also ask

What is new DateTime in PHP?

The DateTime::format() function is an inbuilt function in PHP which is used to return the new formatted date according to the specified format.

What does NOW () return in PHP?

It is a PHP function that "returns the date & time in the same format as the MySQL function NOW()", which is precisely the question.

How can I get the difference between two dates and time in PHP?

PHP date_diff() Function $date2=date_create("2013-12-12"); $diff=date_diff($date1,$date2);


1 Answers

If you'll look into DateTime constructor definition you'll see that now is default value for initializing string:

public DateTime::__construct() ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )

That means both two calls above are always equal. If you'll not specify anything as initializing string, PHP will substitute now implicitly for you.

like image 78
Alma Do Avatar answered Sep 22 '22 13:09

Alma Do