Advertisement
Shaun_B

Database friendly time and date stamp (PHP).

Feb 21st, 2013
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.41 KB | None | 0 0
  1.     // Here is a quick example of getting a Database-friendly time and date stamp,
  2.     // so, for instance, if you were recording when files where stored in a database
  3.     // file, you might:
  4.     /**
  5.      * $dbDate = $this->getTimeAndDate();
  6.      * $query = mysql_query ( "INSERT INTO  `" . $this->dbPrefix . "my_pictures` "
  7.      *          . "(`file_path`, `file_name`, `cat_id`, `date`) VALUES "
  8.      *          . "('" . $this->dir . "', "
  9.      *          . "'" . $this->imgData['fileName'] . "', "
  10.      *          . "'" . $this->imgData['category'] . "', "
  11.      *          . "'" . $dbDate . "')"
  12.      *          );
  13.      **/
  14.     // Here is the function:
  15.     public function getTimeAndDate() {
  16.         $date = getdate();
  17.         $time = '';
  18.         $dbDate = '';
  19.         if((int)$date['hours']<10) {
  20.             $time .= '0' . $date['hours'];
  21.         } else {
  22.             $time .= $date['hours'];
  23.         }
  24.         $time .= ':';
  25.         if((int)$date['minutes']<10) {
  26.             $time .= '0' . $date['minutes'];
  27.         } else {
  28.             $time .= $date['minutes'];
  29.         }
  30.         $time .= ':';
  31.         if((int)$date['seconds']<10) {
  32.             $time .= '0' . $date['seconds'];
  33.         } else {
  34.             $time .= $date['seconds'];
  35.         }
  36.         $dbDate = $date['year'] . '-';
  37.         if((int)$date['mon']<10) {
  38.             $dbDate .= '0' . $date['mon'];
  39.         } else {
  40.             $dbDate .= $date['mon'];
  41.         }
  42.         $dbDate .= '-';
  43.         if((int)$date['mday']<10) {
  44.             $dbDate .= '0' . $date['mday'];
  45.         } else {
  46.             $dbDate .= $date['mday'];
  47.         }
  48.         $dbDate .= ' ' . $time;
  49.         return $dbDate;
  50.     }
  51.     // Simple!
  52.     // @Author: Shaun B
  53.     // @Date:   2013-02-21
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement