11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 12 • DATE AND TIMEwww.it-ebooks.infoWorking with Timestamps<strong>PHP</strong> offers two functions for working with timestamps: time() and mktime(). The former is useful forretrieving the current timestamp, whereas the latter is useful for retrieving a timestamp correspondingto a specific date and time. Both functions are introduced in this section.Determining the Current TimestampThe time() function is useful for retrieving the present Unix timestamp. Its prototype follows:int time()The following example was executed at 15:31:22 EDT on May 24, 2010:echo time();This produces a corresponding timestamp:1274729482Using the previously introduced date() function, this timestamp can later be converted back to ahuman-readable date:echo date("F d, Y h:i:s", 1274729482);This returns the following:May 24, 2010 03:31:22Creating a Timestamp Based on a Specific Date and TimeThe mktime() function is useful for producing a timestamp based on a given date and time. If no dateand time is provided, the timestamp for the current date and time is returned. Its prototype follows:int mktime([int hour [, int minute [, int second [, int month[, int day [, int year]]]]]])The purpose of each optional parameter should be obvious, so I won't belabor each. As an example,if you want to know the timestamp for May 24, 2010 3:35 p.m., all you have to do is plug in theappropriate values:echo mktime(15,35,00,5,24,2010);This returns the following:276

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!