Wickham's HTML & CSS tutorial

Sitemap | Search | PHP introduction

PHP and MySQL date, time and future dates

View in Firefox, Safari, Opera and IE but IE6 often needs different solutions. In general IE7 and IE8 display like Firefox apart from the HTML5 and CSS3 features. The IE9 & above updates are mainly related to HTML 5 and CSS3 issues and display to a large extent like other main browsers. Google Chrome is based on the same WebKit engine as Safari.

Some of the examples are provided just to show problems, others show solutions that work in most major browsers. Use the example that suits you.
red icon problems   gold icon warning: works in some situations or some browsers or needs care to display as you wish   green icon OK in Firefox, Safari, Opera, Google Chrome and IE

PHP date and time and future date

1 green icon Look at php.net for the various date format characters.

The examples below are based on London time. There's not a server-based/PHP method for getting local time. You have to get it from the client via Javascript. Google "bitbucket timezone detect" and use it to set a "local_timezone" cookie that you can read from PHP and set via date_default_timezone_set().

You can use the date format characters in variables like this:-

The output of the four echo statements is:-

27 Apr 2021

where the first is for the current date in the y-m-d format (y-m-d and y/m/d are default formats) and the second is based on the current date, adding 10 days onto the string value of the date (ie onto y-m-d as text, ie 10 days onto a text date in the format 09-10-29) and changing the format.

The same future date can also be expressed as 10 days added on to the current date string in variable $date0:-

PHP future dates sent to a MySQL database

2 green icon The example below has a simple form which displays the date 10 days in the future in a format which is not the default accepted by a MySQL database; it then formats the date into a default style and sends data to a database; the table below the MySQL setup shows the data extracted from the database in another date and time format and ordered by date and time.

Time formats can be coded in the same way as dates. In this example the time in the form has been shown as a fixed value in the format 09:15 instead of the current time and the output in the bottom table has had the format changed because the field type is Time and the database recognises the form value.

You could use this principle where you wanted to show a preferred date and time for a future delivery or appointment, and the viewer can edit the date and time (although if he edits them incorrectly it will show a date of 1970-01-01 and time of 0000:00:00 in the database, but you will have the Timestamp in the database to see the date and time submitted and you can then verify by email).

Dates to database example

The form above has been disabled.

The code for the form and its PHP and MySQL link is:-

See PHP Form to MySQL for details on setting up a MySQL database. The MySQL set up for this example is as follows:-

Field Type Length/ Values Collation Attributes Null Default Extra Action
ID int 5     No   auto_ increment Primary
date1 Date       Yes NULL    
time1 Time       Yes NULL    
place1 Text   utf8_unicode_ci   Yes NULL    
email1 Text   utf8_unicode_ci   Yes NULL    

The table below shows all the data that has dates after the current date (which was 10 November 2009 when I prepared this example but I have disabled $today in this example to continue to show the data) with the Timestamp which is automatically added by the database when the entries are submitted; ordered by the date and time entered by the viewer.

ID Date Time Address Email Timestamp
Access denied for user 'zipiyyor'@'localhost' (using password: YES)