Solved

How can I validate a MYSQL Timestamp with PHP?

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Populate custom post type/custom fields from an external database in WordPress 3 33
Email called spam 5 36
Help with PHP 13 27
Correct syntax for subdirectories 9 20
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
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.

830 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