

$date = new DateTime(" 00:00:00") Īnd here's another that adds a year and a month. I recommend the modify() function because of how easy it is to read, but this is an example of how you add a month to the date. The function requires you to create a new DateInterval class and pass it a string formatted exactly how you want to change. The DateTime class also provides an add and subtract function, which still allows you to modify the DateTime, but in a format that's a little harder to read. The true power of the DateTime class is how easily it lets you manipulate time after you've created it.įor example, if you want to add a month to the current date, you can use the modify() function to do so. Up until this point, most of you have been thinking, so what the date() function can do everything you just showed me. Using the createFromFormat() function, we can specify the format of that date. If we just enter this string into a new DateTime class, we'll end up with November 12th. Now is this November 12, 2008, or December 11, 2008? The worst case for a date is getting something like "11/12/08". Invariably, you're going to have to take input from your user to work with. The complete list of these formats can be found on the PHP website. There are a wide variety of formatting options that can be found in the online documentation, but it also includes a couple of built-in formats like RSS. This is helpful if you want to display the date to a user or save the value to a database or a file.

I've already jumped the gun on this, but the format() function allows you to convert a DateTime object into a string. $date = new DateTime("first day of december") PHP also has a long list of relative date formats so you can generate a DateTime using more readable formats like the following. You can also create a DateTime object for a specific date and time. As a quick aside, the format function converts the DateTime into a string representation of the object. Without any parameters, the DateTime class uses the date and time at the moment that that class was initialized. The easiest way to work with a DateTime class is to just create a new instance of it.
#PHP DATETIME HOW TO#
In this article, we'll discuss how to use PHP's built-in DateTime classes to perform some basic operations on DateTime data. I ended up spending a lot of time fixing bugs in my data handling logic instead of adding features to solve my user's problems.Īs an experienced developer, I know I can reach for PHP's built-in date-time classes or one of the libraries built off of them. You might need to see how far apart two dates are, add several days to the date, or convert a string sent to us by an end user into something we can work with.Īs an inexperienced developer, I had not invented here syndrome for these kinds of problems all the time. The DateTime constructor now includes theĬurrent microseconds in the constructed value.As a developer, you're going to run into situations where you need to perform some kind of operation on a date or time. The class constants of DateTime are now defined
#PHP DATETIME ARCHIVE#
Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Enumerations Errors Exceptions Fibers Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Predefined Attributes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts ? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search
