Getting Date and Time Elements

  $MyString = "This current DateTime: " . date('Y-m-d H:i:s');

  $ModifiedDateTime = date('Y-m-d H:i:s', strtotime($MyDateTime));

  //"Y-m-d H:i:s' gives "2000-01-01 00:00:00"

  $Year = intval(date('Y', strtotime($MyDateTime)));
  $Month = intval(date('n', strtotime($MyDateTime)));
  $DayOfMonth = intval(date('j', strtotime($MyDateTime)));
  $Hour = intval(date('G', strtotime($MyDateTime)));

Parameters you can use

Calculations

Date in # days time
  $project_end_date = date("Y-m-d", mktime(date('H'),date('i'),date('s'), date('m'),date('d') + 30,date('Y')));

Compare Date Time

     $now = strtotime(date("Y-m-d H:i:s"));
     $end = strtotime($UpgradesValidUntil);
     if ($now > $end)
     {
     }


     if ( strtotime($MyDateTimeString1) > strtotime($MyDateTimeString2) )
     {
     }

Adjusting Date Time Values

  $AdjustedTime = date('Y-m-d H:i:s',strtotime('+1 hour +30 minutes +45 seconds',strtotime($MyDateTime)));

  $AdjustedTime = date('Y-m-d H:i:s',strtotime('+1 hour',strtotime($MyDateTime)));

  $AdjustedTime = date('Y-m-d H:i:s',strtotime('-1 hour',strtotime($MyDateTime)));

  $AdjustedTime = date('Y-m-d H:i:s',strtotime("-$MyVariable minutes",strtotime($MyDateTime)));

Values:

  • week
  • day
  • hour
  • minute
  • second

MYSQL Date & Time

To set a DateTime field to now use:

Now()

(Don’t add single quotes around it)

Is String A DateTime?

if (strtotime($CreatedDateTime) !== FALSE)    //Returns timestamp on success, false otherwise
  //Yes string is a date

Convert DateTime

See Strings-Values

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published.