WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Checks Date-Input from HTML-Forms and converts to YYYY-MM-DD Format for MySQL Date-Fields
Categories : MySQL, Date Time, PHP, Databases Update Picture
Alexander Berner
Date : Aug 29th 1998
Grade : 5 of 5 (graded 3 times)
Viewed : 41916
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Alexander Berner
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?
// This function is made to check date-input from an HTML-form
// and bring it to the YYYY-MM-DD format that it can be
// insertet in a Date-Field of a MySQL-Table.
//
// Input-Formats can be:
// "d.m.y" or "y.m.d" or "d.m" or "d"
// if year or month and year are missing, these values are
// taken from the current date.
//
// When the result-String is empty, the date-input was wrong.
//
// Usage: $date_ok = input2date($date_from_form_field);
//
function input2date($idate)
{
$token="-./ ";

$p1 = strtok($idate,$token);
$p2 = strtok($token);
$p3 = strtok($token);
$p4 = strtok($token);

$date="";
$y=""; $m=""; $d="";

// check 'd.m.y'
if (($p1>0 && $p1<32) &&
($p2>0 && $p2<13) &&
($p3>32))
{
$y=$p3;
$m=$p2;
$d=$p1;
}

// check 'y.m.d'
if ($y == "" &&
($p1>32) &&
($p2>0 && $p2<13) &&
($p3>0 && $p3<32))
{
$y=$p1;
$m=$p2;
$d=$p3;
}

// check 'd.m'
if ($y == "" &&
($p3=="") &&
($p2>0 && $p2<13) &&
($p1>0 && $p1<32))
{
$y=date("Y");
$m=$p2;
$d=$p1;
}

// check 'd'
if ($y == "" &&
($p3=="") &&
($p2=="") &&
($p1>0 && $p1<32))
{
$y=date("Y");
$m=date("m");
$d=$p1;
}

// add 1900 or 2000 to year
if ($y!="" && $y<=99)
{
if ($y>=70) $y = $y + 1900;
if ($y<70) $y = $y + 2000;
}

if ($y!="")
{
if (checkdate($m, $d, $y))
$date="$y-$m-$d";
}

return $date;
}
?>




Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP, Arrays, Date Time, Databases, MySQL
PHP Calendar Web App
Categories : PHP, Databases, MySQL, Date Time, Calendar
Phorum, MySQL, Language, UK date format, MySQL UK Date format
Categories : PHP, Date Time, Strings, MySQL, Databases
A php function to convert a mySQL timestamp(10) to a european readable format
Categories : MySQL, Date Time, PHP, Databases
In Mysql, the 'datetime' data type is used to store date that looks like '2007-10-16 16:26:30'. we are going to design a function called mysqldate() that takes a mysql date and output format as input and outputs a well formated date.
Categories : PHP, Date Time, Databases, MySQL
List people whose birthdays fall on the current Day and Month
Categories : Databases, Date Time, MySQL, PHP
Data Retrieve from Mysql using AJAX with PHP
Categories : PHP, AJAX, Date Time, Databases, MySQL
formating MySQL Timestamp to get a propper Output. related : Timestamp, ereg_replace, mysql, substr
Categories : MySQL, PHP, Date Time, Databases
Count how many weeks in the month have a specified day, such as Mon, Tue, etc. Var avail - number of days - first day name of the month, occurrences of Sun, occurrences of Mon, etc. Allows you to calculate number of working hours exclude Holidays.
Categories : Calendar, Date Time, PHP, Databases, MySQL
bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
How to Insert a Date Format Into MySQL from PHP
Categories : PHP, Databases, MySQL, Date Time, Beginner Guides
mysql date/time converters
Categories : PHP, MySQL, Databases, Date Time
Finding the day of the week for a specific date.
Categories : PHP, Databases, MySQL, Date Time
This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
Simple function to return the number of days in a time span between 2 given dates.
Categories : PHP, Date Time, MySQL, Databases
 Slava Knyazhevsky wrote : 39
<? 
//   int DateInput(string datestring)
// This function is made to check date-input from an 
HTML-form 
// and bring it to the Unix timestamp  format that it can 
be 
// insertet in a date() function
// 
// Input-Formats can be: 
// "d.m.y" or "m.d.y" or "y.m.d" or "d.m" or "d" 
// if year or month and year are missing, these values 
are 
// taken from the current date. 
// 
// When the result == -1, the date-input was wrong. 
// 
// Usage: $timestamp = DateInput
($date_from_form_field); 
// 
function DateInput($idate) { 
  
$token="-./ "

  
$p1 strtok($idate,$token); 
  
$p2 strtok($token); 
  
$p3 strtok($token); 
  
$p4 strtok($token); 

  if ( 
$p3=="" ) { 
    
$p3=date("Y"); 
    if ( 
$p2=="" $p2=date("m"); 
  }

  
// add $p1y and $p3y in year range
  
$p1y $p1;
  if ( 
$p1y<=99 ) { if ( $p1y<70 $p1y += 2000; else 
$p1y += 1900; }
  
  
$p3y $p3;
  if ( 
$p3y<=99 ) { if ( $p3y<70 $p3y += 2000; else 
$p3y += 1900; }

  
// check 'd.m.y' 
  
if ( checkdate($p2$p1$p3y) ) return mktime
(0,0,0,$p2$p1$p3y); 

  
// check 'm.d.y' 
  
elseif ( checkdate($p1$p2$p3y) ) return mktime
(0,0,0,$p1$p2$p3y); 

  
// check 'y.m.d' 
  
elseif ( checkdate($p2$p3$p1y) ) return mktime
(0,0,0,$p2$p3$p1y); 

  return -
1;

?> 
 
 Christian Weinert wrote :456
The function won`t work for Years smaller than 33. If you enter `16.09.00` the function returns nothing.
That`s because in the check for `d.m.y`, `16.09.00` returns $p3=0 and the clause is $p3&gt;32.

Change the function as below, substituting `&gt;32` with `&gt;=0` and it will work perfectly!

 // check `d.m.y` 
  if (($p1&gt;0 && $p1&lt;32) && 
      ($p2&gt;0 && $p2&lt;13) && 
      ($p3&gt;=00))  //change this one
  { 
    $y=$p3; 
    $m=$p2; 
    $d=$p1; 
  } 

  // check `y.m.d` 
  if ($y == "" && 
      ($p1&gt;=00) &&  //and this one
      ($p2&gt;0 && $p2&lt;13) && 
      ($p3&gt;0 && $p3&lt;32)) 
  { 
    $y=$p1; 
    $m=$p2; 
    $d=$p3; 
  }