Verifying Date/Time Input in PHP - php

New to programming in PHP. Trying to verify input format for a date/time. User input is as follows for Nov 27 2012 at 6 PM '2012-nov-27|6pm'.
Not really sure where to start. Any suggestions? Thanks.

Have a look here for date_parse_from_format documentation and here for general date formatting. Give this a try:
<?php
$date = "2012-nov-27|6pm";
print_r(date_parse_from_format("Y-M-d|ga", $date));
?>

Gangnam OOP style:
$input = '2012-nov-27|6pm';
$date = \DateTime::createFromFormat('Y-M-j ga', str_replace('|', ' ', $input));
if ($date === false) {
throw new \Exception('Invalid date!');
}
NOTE: I experienced an issue by using | in format/date string, so that the str_replace()
NOTE 2: If input day format is 01-31 instead of 1-31, replace the j with a d in createFromFormat() first parameter.

You can use checkdate:
if(checkdate($month, $day, $year)){
echo 'Valid date!';
}
Or, you can convert whatever the user enters with strtotime(). If strtotime can't determine what the date is, it returns bool false.
if(strtotime($dateEntered) !== false){
echo 'Valid date!';
}

Try:
$datetime = "2012-nov-27|6pm";
$unixtime = strtotime( $datetime );
if( is_numeric($unixtime) && $unixtime <= PHP_INT_MAX ) {
//do something if true
} else {
echo "Invalid DateTime";
}
Demo

If you're just looking to test your date for validity, the following will work. Even if you were just looking to test the input format, I suggest you also sanity check the data as well.
$thedate = "2012-nov-27|6pm";
$arryParts = explode("|", $thedate);
$arryDate = date_parse($arryParts[0]);
if(!checkdate($arryDate['month'], $arryDate['day'], $arryDate['year'])) {
/* error handling */
}
/* valid date, continue processing */

If you just want to check the format, use a regex:
$months = 'jan|feb|mar|apr|may|jun|jul|aug|sep|okt|nov|dec';
$pattern = '/([0-9]{4})-('.$months.')-([0-9]{2})\\|([0-9]?[0-9])(am|pm)/';
preg_match($pattern, $input, $matches);
var_dump($matches);
If you really want to check if a specific date/time exists, thing become more complicated. I may be wrong, but I don't think it can be done using PHP date functions, as they are quite tolerant to input errors. Maybe there's a library to do this.

Related

Checking if a string has 07.05.2013 date format

Trying to validate a string to check if it has the format 07.05.2013, not sure how to approach it.
Thinking of checking if '.' are the 3rd and 6th characters, then checking if the rest of the characters are digits but I don't know how to achieve that.
If you simply need to parse the date, you can use the date time features of php.
<?php
$date = DateTime::createFromFormat('d.m.Y', '07.05.2013');
echo $date->format('Y-m-d');
?>
Use the DateTime::CreateFromFormat() method. This will validate your input and create a date object at the same time (which you can then use to work with the date).
$dateObj = DateTime::CreateFromFormat('d.m.Y', $inputString);
If the date is invalid or is in the wrong format, $dateObj will be false.
If it is a valid date in the required format, $dateObj will be a DateTime object.
Hope that helps.
if (preg_match('/^\d{2}\.\d{2}\.\d\{4}$/', $yourstring)) {
...
}
This will be true if your string matches expression like dd.dd.dddd where d is a digit.
You can also use the checkdate function:
$date= '07.05.2013';
$date_arr= explode('.', $date);
if (checkdate($date_arr[0], $date_arr[1], $date_arr[2])) {
// validate your date here
}
Something like:
$date = DateTime::createFromFormat('d.m.Y', $yourStringWhichMightBeADate);
if ($date)
{
// it's a date, so use it
}
Or:
$date = DateTime::createFromFormat('m.d.Y', $yourStringWhichMightBeADate);
if ($date)
{
// it's a date, so use it
}
if the month is first rather than the day of month.
SOLUTION 1:
Here's the way I did it, it works with every input you decide to enter (e.g: "12.02.1996", "12.30.1996", "dasdsadas", and so on..)
public function valid_date($inputdate){
$date = $inputdate;
if (strtotime($date)){
if (strpos($date,'.') !== false) {
list($day, $month, $year) = explode('/', $date);
return checkdate($month, $day, $year);
}else{
return false;
}
}else{
return false;
}
}
If input date is 10/10/1996 which is also a valid format, or 10/02/1996, it won't accept them because I'm asking the user to use the format with ".". Just remove the "if" if you don't want to do this validation and that is it.
SOLUTION 2:
Found this at php.net, very clean and interesting!
public function valid_date($date, $format = 'd.m.Y'){
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
}

strtotime() converts a non existing date to another date

I am building a timestamp from the date, month and year values entered by users.
Suppose that the user inputs some wrong values and the date is "31-02-2012" which does not exist, then I have to get a false return. But here its converting it to another date nearby. Precisely to: "02-03-2012"..
I dont want this to happen..
$str = "31-02-2012";
echo date("d-m-Y",strtotime($str)); // Outputs 02-03-2012
Can anyone help? I dont want a timestamp to be returned if the date is not original.
You might look into checkdate.
That's because strtotime() has troubles with - since they are used to denote phrase like -1 week, etc...
Try
$str = '31-02-2012';
echo date('d-m-Y', strtotime(str_replace('-', '/', $str)));
However 31-02-2012 is not a valid English format, it should be 02-31-2012.
If you have PHP >= 5.3, you can use createFromFormat:
$str = '31-02-2012';
$d = DateTime::createFromFormat('d-m-Y', $str);
echo $d->format('d-m-Y');
You'll have to check if the date is possible before using strtotime. Strtotime will convert it to unix date meaning it will use seconds since... This means it will always be a date.
You can workaround this behavior
<?php
$str = "31-02-2012";
$unix = strtotime($str);
echo date('d-m-Y', $unix);
if (date('d-m-Y', $unix) != $str){
echo "wrong";
}
else{
echo date("d-m-Y", $unx);
}
or just use checkdate()
Use the checkdate function.
$str = "31-02-2012";
$years = explode("-", $str);
$valid_date = checkdate($years[1], $years[0], $years[2]);
Checkdate Function - PHP Manual & Explode Function - PHP Manual
Combine date_parse and checkdate to check if it's a valid time.
<?php
date_default_timezone_set('America/Chicago');
function is_valid_date($str) {
$date = date_parse($str);
return checkdate($date['month'], $date['day'], $date['year']);
}
print is_valid_date('31-02-2012') ? 'Yes' : 'No';
print "\n";
print is_valid_date('28-02-2012') ? 'Yes' : 'No';
print "\n";
Even though that date format is acceptable according to PHP date formats, it may still cause issues for date parsers because it's easy to confuse the month and day. For example, 02-03-2012, it's hard to tell if 02 is the month or the day. It's better to use the other more specific date parser examples here to first parse the date then check it with checkdate.

Validate input is in YYYY-MM-DD HH:mm:ss format?

I'm writing a PHP script that takes in a user entered value that MUST be something like this.
2011-06-17 00:00:00
...
2011-06-17 23:59:59
How do I verify that it is indeed a correct input?
Alternatively, use strtotime() on the input, and then date() to put it in your required format. This has the advantage of validating that the user provided a correct date, not just a correct format. I.e., a regex check won't catch when somebody puts in Feb 31.
$date = strtotime($input);
if ($date === false) {
throw Exception('bad date');
}
$formatted = date('<whatever>', $date);
You're looking for validation for ISO 8601.
Here is a sample regular expression to validate that format:
^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$
http://www.pelagodesign.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/
or better yet,
Zend Framework Date Validation
Example:
$validator = new Zend_Validate_Date(array('format' => 'yyyy-MM-dd HH:mm:ss'))
$validator->isValid('2011-06-17 00:00:00');
Use DateTime::createFromFormat and check for return value.
/(\d{4}[\-]((0[1-9]|1[0-2]))[\-]((0[1-9]|1[0-9]|2[0-9]|3[0-1]))(([t-tT-T]|\s)((0[0-9]|1[0-9]|2[0-3]))[\:]((0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]))[\:]((0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]))([.][a-zA-Z0-9]*)?)?)$/i;
Its try for
2013-06-28
2013-06-28T13:35:59
2013-06-28 13:35:59
2013-06-28T13:35:59.000
2013-06-28 13:35:59.000
2013-06-28T13:35:59.000Z
2013-06-28 13:35:59.000Z
Its not simplified, although it can greatly be simplified..
use regular expressions: http://php-regex.blogspot.com/ and http://networking.ringofsaturn.com/Web/regex.php are good places to start learning
or use this solution:
$exploded = explode($INPUT_STRING, " ");
$date_explode = explode($exploded[0],"-");
$time_explode = explode($exploded[1],":");
if (empty($date_explode[0])||empty($date_explode[1])||empty($date_explode[2])||empty($time_explode[0])||empty($time_explode[1])||empty($time_explode[2])) {
die ("ERROR! Not correct input format!");
}
$date = strtotime('2011-13-17 23:00:00');
if($date){print("legal");}
For complete date/time verification, use both DateTime::createFromFormat() and strtotime(), such as
// Convert it to test if the datetime can be successfully used. Finer than regex.
$dateformat = DateTime::createFromFormat('Y-m-d H:i:s', '2014-09-27 20:00:05');
$datereal = strtotime($inputStart);
if( $dateformat === FALSE || $datereal === FALSE )
echo "Invalid format/datetime".
If you want, you can divide the checks to post separate messages if the date is wrong format or simply impossible.

Validating a date dd/mm/yyyy

Does anyone know of any good functions for validating a date via input text box in codeigniter like this: dd/mm/yyyy? Thanks
You're probably looking for the strptime function. For the specified format you can use it like this:
$result = strptime($input, '%d/%m/%Y');
$resultwill be either false if the input is invalid or on array with the date.
Or if you're on Windows or have PHP >= 5.3, consider using date_parse_from_format.
Use strtotime function.
$date = strtotime($input);
returns false if invalid, or -1 for php4.
you could also use a custom callback function
<?php
function valid_date($date)
{
$date_format = 'd-m-Y'; /* use dashes - dd/mm/yyyy */
$date = trim($date);
/* UK dates and strtotime() don't work with slashes,
so just do a quick replace */
$date = str_replace('/', '-', $date);
$time = strtotime($date);
$is_valid = date($date_format, $time) == $date;
if($is_valid)
{
return true;
}
/* not a valid date..return false */
return false;
}
?>
your validation rule looks like:
$rules['your_date_field'] = "callback_valid_date";
dont know about codeigniter but there's some alike already in php already
checkdate();

compare date trimming

I have a field (nonTimeStampDate) that has date like this
2010-03-15
and I want to check it against another field (timeStampDate) which is
2010-03-15 15:07:45
to see if the date matchs. But as you can see since the format is different it doesnt match even though the date is same.
Any help will be appreciated.
thanks
My first thought is using date() and strtotime() to reformat them.
$date1 ="2010-03-15";
$date2 = "2010-03-15 15:07:45";
if (date('Y-m-d', strtotime($date1)) == date('Y-m-d', strtotime($date2)))
{
//do something
}
This would work and give you more flexibility in how the two dates formatted to begin with. Not the most elegant.
You might want to try this code:
if (strpos($date1, $date2) !== false) {
// Your code here
}
It is a bit faster than exploding the value by a space as suggested by Anax. Make sure that $date2 contains the shorter of the two dates.
If you are certain about the input string format, you need to split it and take the first part, in order to compare it with your original date:
$splits = explode(' ', $original);
$datapart = $splits[0];
if ($datepart == $nonTimeStampDate) {
// your code here
}
What Anax says, or if these values are in MySQL tables, you can use MySQLs datetime functions (like DATE()) to compare them in MySQL.
Then, just compare the date part:
<?php
if( substr('2010-03-15 15:07:45', 0, 10) == '2010-03-15' ){
echo 'Dates match';
}
?>
Whatever, if you need to do serious date handling, you need to use a proper format, such as a DateTime object.
$firstDate = date("Y-m-d", strtotime("2010-03-15"));
$secondDate = date("Y-m-d", strtotime("2010-03-15 15:07:45"));
if( $firstDate == $secondDate ) {
// true
}

Categories