Solved

How can I validate a MYSQL Timestamp with PHP?

Posted on 2008-10-11
2
855 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

710 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