Solved

How can I validate a MYSQL Timestamp with PHP?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
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 …

895 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

13 Experts available now in Live!

Get 1:1 Help Now