One of the things I've excluded up to this point is the fact that the DateTime constructor has a second optional parameter that determines the time zone that the DateTime exists within. Time zones can be a real pain to handle, but using the DateTime class, makes it a lot easier. "%y years %m months %d days %H:%i:%s and %R%a total days” output: string(51) "1 years 1 months 24 days 01:1:1 and +420 total days" It's important to note it uses the percent sign to show where the variables need to go, and it also adds a %R option, which will tell you if there's a positive or negative difference between two dates. It also has a format() function that will allow you to create a formatted version of the same information. If you need to determine the difference between two days, you can use the diff() function to create a DateInterval object, which will then allow you to see the difference between those two dates. $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. 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. convert m-d-y or m.d.As a developer, you're going to run into situations where you need to perform some kind of operation on a date or time. * string $d various representations of date * Unsupported formats or invalid dates will generate an Exception * Changes PHP's default behaviour for dates with dashes or dots. * Accept dates in various m, d, y formats and return as Y-m-d Or, you can make a function to accept m-d-Y and output Y-m-d: /** convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y (substr avoids microtime error) Public function _construct(string $time="now", DateTimeZone $timezone = null) * Quietly convert European format to American format If you wish to accept dates using American ordering (month, date, year) for European style formats (using dash or period as day, month, year) while still accepting other formats, you can extend the DateTime class: /**
0 Comments
Leave a Reply. |