Solved

How can I validate a MYSQL Timestamp with PHP?

Posted on 2008-10-11
2
837 Views
Last Modified: 2013-12-13
I have a page that builds what I believe is a UNIX Timestamp, in the following format:

YYYYMMDDHHMMSS

My PHP script fails to insert the date into MySQL if a date that doesn't exist, such as February 31st, is selected.  Does PHP have a built in function to do this kind of validation or do I have to create my own function?
$invoiceTimeStamp  = $_POST['invoiceYear'];

$invoiceTimeStamp .= $_POST['invoiceMonth'];

$invoiceTimeStamp .= $_POST['invoiceDay'];

$invoiceTimeStamp .= "000000"; // zero hour, minute, and second

Open in new window

0
Comment
Question by:CheeserTheAverage
  • 2
2 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22695461
First, this is not a unix timestamp. A unix timestamp is an integer, the number of seconds since 1/1-1970.

You could use the PHP function mktime() to validate.

http://php.net/manual/en/function.mktime.php

Note that this will only work for dates between 1970 and 2038 on some systems (windows before PHP 5.1), and for dates between 1901 and 2038 in newer PHP versions (5.1+).
function ValidDate($y,$m,$d) {

  if(strlen($m)<2) $m = '0'.$m;

  if(strlen($d)<2) $d = '0'.$d;

  return (date('Y-m-d',mktime(0,0,0,$m,$d,$y))=="$y-$m-$d");

}

Open in new window

0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 22695502
I just found out PHP has a builtin function for this: checkdate()

http://php.net/checkdate
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now