Date functions with PHP

Date and time format - ISO 8601

What is ISO 8601?

ISO 8601 describes an internationally accepted way to represent dates and times using numbers.

When dates are represented with numbers they can be interpreted in different ways. For example, 01/05/12 could mean January 5, 2012, or May 1, 2012. On an individual level this uncertainty can be very frustrating, in a business context it can be very expensive. Organizing meetings and deliveries, writing contracts and buying airplane tickets can be very difficult when the date is unclear.

ISO 8601 tackles this uncertainty by setting out an internationally agreed way to represent dates:

YYYY-MM-DD

For example, September 27, 2012 is represented as 2012-09-27.

To get the current date in PHP, use the function now() :

function now() {
    return date('Y-m-d');
}

Sometimes date values has to be stored in a different format. Reformatting the date with a function is simple:

# get the current date
$now = now();

function sdate_format ( $date) {
    return substr($date,8,2) . substr($date,5,2) . substr($date,0,4) ;
}
# reformat
$sdate = sdate_format($now);

With a given current date of 2012-09-27 this would become 27092012



TITLE: Date functions with PHP
DATE: 2016-07-15
AUTHOR: jRu
TAGS:
date php