code snippet: isDateTime() yyyy-mm-dd hh:mm:ss

Include file is found in this directory of the website:

/web/includes/generalFormFuncs.php

Usage Example:

if ( strlen($timedate) > 0 and !isDateTime($timedate)) { return false; }

Date-Time Format:

ccyy-mm-dd hh:mm:ss

Code For isDateTime Function:

function isDateTime($dateTime_in) {
/*
Purpose: Return truth about $dateTime_in. Is it a MySQL datetime string formatted
as ccyy-mm-dd hh:mm:ss ... huh?
Author: Sameh Labib
Date: 09/16/2010
*/

  $strIsValid = TRUE;
  
  if ( $dateTime_in == "0000-00-00 00:00:00" ) { return $strIsValid; }

  // check the format first
  if (!ereg("^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$", $dateTime_in)) {
    $strIsValid = FALSE;
  } else {
    // format is okay, check that years, months, days, hours, minutes , seconds
    // are okay
    $dateTimeAry = explode(" ", $dateTime_in); // break up string by space into date, time
    $dateStr = $dateTimeAry[0];
    $timeStr = $dateTimeAry[1];
    
    $dateAry = explode("-", $dateStr); // break up date string by hyphen
    $yearVal = $dateAry[0];
    $monthVal = $dateAry[1];
    $dayVal = $dateAry[2];
    
    $timeAry = explode(":", $timeStr); // break up time string by colon
    $hourVal = $timeAry[0];
    $minVal = $timeAry[1];
    $secVal = $timeAry[2];
    
    $dateValIsDate = checkdate($monthVal, $dayVal, $yearVal);
    
    if ($hourVal > -1 && $hourVal < 24 && $minVal > -1 && $minVal < 60
        && $secVal > -1 && $secVal < 60) {
      $timeValIsTime =  TRUE;
    } else {
      $timeValIsTime =  FALSE;
    }

    if(!$dateValIsDate || !$timeValIsTime) {
      $strIsValid = FALSE;
    }
  }

  return ($strIsValid);
}
Advertisements

About samehramzylabib

See About on https://samehramzylabib.wordpress.com
This entry was posted in PHP Code Snippets, PHP Data Value Types, PHP Form Processing, PHP Time Functions and tagged , , , , . Bookmark the permalink.

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s