![]() ![]() This function is handy for calculating how many days an event happened in the past or will happen in the future (i.e., a birthday, holiday, anniversary, etc…). MySQL’s DATEDIFF() function returns the number of days between 2 date values. Read more in the official date_diff() documentation.) DATEDIFF() (Note: date_diff() is in the procedural style. $num_mnths = date_diff($current_date, $six_mnths_later) Įcho $num_mnths->format('%m months') 5 months Using the m format, we can know the number of months difference between the date objects: $current_date = date_create('') The a format echoed in the browser returns: +21 days $num_days = date_diff($current_date, $xmas_date) I’ll create 2 dates and then call the format() method on the resulting object from the date_diff() function call: $current_date = date_create('') Syntax PHP date_diff() date_diff($origin_date, $target_date) The difference can be a number of different values such as days, months, or even hours (and many more). PHP’s date_diff() function returns the difference between 2 DateTime objects. (Visit this MySQL DATE_FORMAT() resource for more information.) There are format specifiers for that pattern as well: mysql> SELECT DATE_FORMAT(last_update, '%d-%b-%y') AS frmt If these values were not already stored as such, you can easily format them with the format specifiers shown in the next query: mysql> SELECT DATE_FORMAT(last_update, '%Y-%m-%d') AS frmtĪ common format you see is the ‘DD-MON-YY’ format (a lot of Oracle SQL dates are stored in this format). Syntax MySQL DATE_FORMAT(): DATE_FORMAT(date, format)įor simplicity’s sake, I am using the ‘store’ table from the sakila practice database with this data: mysql> SELECT last_updateįor a 3-letter month abbreviation, a 2-digit day – with the suffix – and the full 4-digit year, we can use these format specifiers: mysql> SELECT DATE_FORMAT(last_update, '%b %D, %Y') AS frmtĪs you have seen in the 1st exploratory query on the ‘store’ table, the ‘last_update’ column date values are stored in a ‘YYYY-MM-DD’ format. Visit the date_format() documentation to see them all.) DATE_FORMAT() (There are several format options for PHP’s date_format(). These format strings shown in the following example produce that format for you: echo date_format($some_date, 'd/m/Y') Oftentimes, you see dates in the 2-digit month 2-digit day, and 4-digit year format (MM/DD/YYYY). In order to format the date value to a full month name, 2-digit day (with suffix), and 4-digit year, we can use these format parameters: echo date_format($some_date, 'F jS, Y') ![]() Using the date_create() function, I make a practice date we can use for the examples: $some_date = date_create('') Returns a date as specified by the format. ![]() Syntax PHP date_format(): date_format($object, $format) Instead, I’ll cover a couple of the common ones I am familiar with and leave the exploration up to you. ![]() With that being said, you can use either of these functions and put date data in pretty much any format you need.īoth functions have a myriad number of different formatting options and covering them all in one blog post would be convoluted at best. In reality, we know better and that normally, data is formatted (if at all) in a way that we can’t use or don’t wish to use. We always hope that the data comes to us in the format we need. PHP: date_sub() | MySQL: DATE_SUB() PHP: date_format() | MySQL: DATE_FORMAT().PHP: date_format() | MySQL: DATE_FORMAT(). ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |