![]() MySQL 5.7 do not allow timezone in the datetime by default. This is ISO 8601 date (added in PHP 5) which MySQL uses Edit One more answer I find easy to use: echo date('c') 'date_time' => date_create('now')->format('Y-m-d H:i:s'), // uses the default timezone I use this approach for my projects if I have to fill an array. ![]() ![]() $elapsedTimeA Ĭase date_create(), elapsed time in seconds: 31 However, here is the benchmark test: $start = time() Įcho 'Case date_create(), elapsed time in seconds: '. Which might never happen in a real-life situation. However, it's only visible if you would instantiate DateTime millions of times. The function date() has a slightly better performance than date_create()->format(). $dt = date_create() // in or not in a namespace it works in both situationsĭisadvantages of using date_create()->format() instead date() By using the date_create() constructor function you don't have to worry about namespaces. There is nothing wrong with this, but the downside of the above is that people forget sporadically about the backslash. Forgetting about the backslash results in a fatal error. The backslash must be used if you are in a namespace. If you work in a namespace and want to initialise a DateTime object with the new keyword, then you have to do it like this: namespace my_namespace The favorable facts of date_create() over new DateTime() are: $nowLongyearbyen = date_create('now', timezone_open('Arctic/Longyearbyen'))->format('Y-m-d H:i:s') yes if you run an international e-commerce platform and do something bound to local time, for example: calculate delivery date-time. Have you ever heard about Longyearbyen? If not, then don't worry, neither did I until I read the official PHP documentation. The funny thing is that it even supports the Arctic circle, and Antarctica. This problem doesn't exists if you are using date_create()->format() in combination with timezone_open(). The downside of that is that it will affect all date/time functions. Example:ĭate_default_timezone_set('Asia/Tokyo'). It is possible to change the timezone during run-time. When timezones matter then the usage of date_create()->format() makes a lot more sense then date() because date() uses the default time zone which is configured in php.ini at the date.timezone directive. The function date() just gives you a string.ĭate_create() accepts a relative date/time format (like now, yesterday or +1 day) see this link, example: $tomorrow = date_create('+1 day')->format('Y-m-d H:i:s') ĭate() accepts a relative date/time format as well, like this: $tomorrow = date('Y-m-d H:i:s', strtotime('+1 day')) The documentation of that Object gives the programmer a quick view that describes the possibilities. The function date_create() is a constructor function for the DateTime object. The favorable facts of date_create('now')->format('Y-m-d H:i:s') over date('Y-m-d H:i:s') are:ĭisadvanteages of using date_create()->format() instead date() summaryĪdvantages of using date_create()->format() instead date() detailed I guess the MySQL NOW() function gets it's format from the datetime_format variable.Īdvantages of using date_create()->format() instead date() summary The variables up here are read-only variables. ![]() Īn interesting fact is that it's possible to get the datetime format by running this query: SHOW VARIABLES LIKE 'd%e_format', the result could be something like this: Variable_name Value The MySQL function NOW() gives the dateTime value in this format: 'YYYY-MM-DD HH:MM:SS'. I think that date_create()->format('Y-m-d H:i:s') is the best way because this approach allows you to handle time/time-zone manipulations easier than date('Y-m-d H:i:s') and it works since php 5.2. $y2015 = date_create_from_format('U', $unixTimeStamp, timezone_open('Europe/Amsterdam'))->format('Y-m-d H:i:s') So if you work with Unix timestamps then you could use: date_create_from_format(). Fatal error: Call to a member function format() on boolean and if you try to invoke format() on a FALSE then you'll get a: Using date_create() with a Unix timestamp will give you a FALSE, $now = date('Y-m-d H:i:s') // Slightly higher performance, but less usable for date/time manipulations $now = (new DateTime('now'))->format('Y-m-d H:i:s') // works in php 5.4 and higher $now = new DateTime('now')->format('Y-m-d H:i:s') // syntax error!!! $now = date_create()->format('Y-m-d H:i:s') // also works in php 5.2 ![]() $now = date_create('now')->format('Y-m-d H:i:s') // works in php 5.2 and higher Here is a list of ways in PHP that mimic the MySQL NOW() function. The mimicry of the MySQL NOW() function in PHP Short answer $now = date_create()->format('Y-m-d H:i:s') ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |