Solved

How can I validate a MYSQL Timestamp with PHP?

Posted on 2008-10-11
2
859 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

615 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