PHP Date & Time format Related

Set timezone:

date_default_timezone_set("Asia/Taipei");
date_default_timezone_set("UTC")
date_default_timezone_set("America/Los_Angeles")

Today:

date('Y-m-d H:i:s', time())

https://www.php.net/manual/en/function.date.php

Add 1 day:
date(‘Y-m-d G:i:s’, strtotime($fromDate) + 60*60*24) (Not recommended)
Solution:

$current_time = strtotime($current_date);
$current_time = strtotime("+1 day", $current_time);
$current_date = date('Y-m-d', $current_time);
  • note: timestamp – the number of seconds since January 1 1970 00:00:00 GMT (millisecond in javascript)
  • Get current Time: time();
  • 日期比較:
    SELECT *
    FROM `form_contact_log`
    WHERE `created_time` >= date_format( ‘2011-5-1’, ‘%Y-%m-%d’)
    Note: 一定要加單引號
  • Make time:$tomorrow = mktime(0,0,0,date(“m”),date(“d”)+1,date(“Y”));
  • Validate a date string: strtotime(string)
    If the string is not a date string, the result will be “false”
    (Javascript: new Date(‘2012-10-40’) == ‘Invalid Date’ )
  • Dispaly time only:
    date(‘g:i A’, strtotime($cell))

4 thoughts on “PHP Date & Time format Related

  1. “hey there, this might be little offtopic, but i am hosting my site on hostgator and they’ll suspend my hosting in 4days, so i would like to ask you which hosting do you use or suggest?”

  2. I’m excited to uncover this great site. I wanted to thank you for ones time for this particularly wonderful read!! I definitely appreciated every little bit of it and I have you book marked to look at new stuff on your site.

Comments are closed.