PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 5, 2022

[FIXED] How to check date type in a variable in PHP

 October 05, 2022     codeigniter, datetime, php, phpexcel     No comments   

Issue

I have an array in which dates are likes 01/10/2015 22:53:00 or 08/19/2016. i want to check that whether it is a date or not. If somebody put a text or other type in the field(Excel array), which i am putting into a variable, then it should generate an error that this is not a date.

I have searched but did not found success. Any help would be appreciated.


Solution

Try this (simple preg_match):

//your date
$date = '01/10/2015 12:12:23';

//check format
if(preg_match("/^[0-9]{1,2}\\/[0-9]{2}\\/[0-9]{4}$/", $date) OR preg_match("/^[0-9]{1,2}\\/[0-9]{2}\\/[0-9]{4} [0-9]{2}\\:[0-9]{2}\\:[0-9]{2}$/", $date)) {
    echo 'OK';
}else {
    echo 'BAD';
}


Answered By - Arkadiusz G.
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing