I'm using php, and need to parse a date string formatted as dd/mm/yyyy and store it in MySql.
How can I convert the string to a datetime variable in a MySql table?
Probably the best way would be using this: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y');
-> '2004-04-31'
Or equivalent PHP functions like:
http://www.php.net/manual/en/function.date-parse-from-format.php (from PHP 5.3)
A generic PHP function would look like
function convertDate($dateString) {
return date('Y-m-d H:i:s',strtotime(str_replace('/','-',$dateString)));
}
or
function convertDate($dateString) {
$a = explode($dateString('/'));
return "{$a[2]}-{$a[1]}-{$a[0]} 00:00:00";
}
First of all you should store a configuration for the required format date, maybe something like this:
Knowing that the ISO_DATE FORMAT is "Y-m-d"
You must save the output configuration somehow, at least the separator.
If you know the separator and you know the format into which the date is entered than you can validate it using checkdate(), and also transform it into the ISO standard by exploding the values by the predefined separator.
I have a validation object that tells me if a field is of a certain type (String,Date,Datetime,Integer,Float) then formats the parameters sent to SQL :
as an example, let's say i get this array from my html form into PHP:
$_POST["DATA"] = array("name"=>"Koko bongo","age"=>12,"graduation_date"=>"12/06/1986");
and we define a validation array, like this:
$validator= array("name"=>"string","age"=>"integer","graduation_date"=>"date");
I have a configuration for each table which makes this automated but you can do it customly inplace by having an evalAndFormatType function that works like this
function evalAndFormatType($value,$type) {
switch strtolower($type) {
case "integer":
$item = is_numeric($value) && !strstr($value,DECIMAL_SEPARATOR) ? intval($item) :false;
break;
case "Date":/*we suppose that somehow we now the order, how you do it it is your decision: configuration array,constants etc*/
$check = explode(DATE_SEPARATOR,$value);
$item = sizeof($check) == 3 && checkdate(intval($check[1]),intval($check[0]),intval($check[2])) ? $check[2]."-".$check[1]."-".$check[0] : false;
break;
default:
throw Exception("Unknown type ".$type);
break;
}
return $item;
}
now, in your code you can say
$_DATA = $_POST["DATA"]; // the previously defined array
$errMsg = array();
foreach($_DATA as $k=>$v) {
$_DATA[$k] = evalAndFormat($v,$validator[$k]);
if($_DATA[$k] === false) {
$errMsg[$k] = "requires type ".$validator[$k];
}
}
if(empty($errMsg)){
$sql= ....WHATEVER USING THE INFO
} else {
return $errMsg;
/*this can be used to show errors nicely near your text boxes by parsing the expected keys or sending it by JSON etc
or
you could create a string out of it by using locale data that you might have which could output something like
Required fields: Graduation date[invalid type],Name,Age ... etc
"/
I hope this answers your question and also explains my "strange" approach on it.
}
Related
I am 2 days into learning PHP and frustratingly I am struggling to GET and use a parameter passed into my API.
I have read the PHP $_GET documentation, it didn't take long, and also a number of SO pages about $_GET.
My use case is simple. I want to retrieve a list of records from MySQL db if they have a modified date greater than the passed in date. The function works if I hard code '2019-03-18 00:00:01' for example.
Using echo I can see I am getting the parameter.
In fact, copying the output from echo and using to update the function which I use in the argument returns the expected result.
I'm guessing this has been asked before but I can't find it. All the examples I read seem more challenging i.e. multiple variables or challenges with the function etc.
I must have made a newb mistake somewhere.
<?php
//Returns Lab Results Modified after the passed in date
include_once 'db_functions.php';
$db = new DB_Functions();
//Get JSON posted by Android Application
if (isset($_GET['date'])) {
$json = $_GET['date'];
echo $json;
// commenting out the next two line results in
// '2019-03-19 00:00:01' returning and not the json array.
$json = '2019-03-18 00:00:01';
echo $json;
$mod = $db->getLabResultsModifiedAfter($json);
$a = array();
$b = array();
if ($mod != false){
while ($row = mysqli_fetch_array($mod)) {
$b["ID"] = $row["ID"];
$b["last_modified"] = $row["last_modified"];
$b["LabRef"] = $row["LabRef"];
array_push($a,$b);
}
echo json_encode($a);
}
} else {
// Fallback behaviour goes here
}
?>
I haven't developed the app side function yet. I am using postman to pass in date of '2019-03-18 00:00:01' i.e. this for local.
Answer as provided by #aisby. I was using quotes in the passed in parameter.
The date you are passing in your URL does not following the format
'2019-03-18 00:00:01'. Try navigating to this URL ...
localhost:8080/its/… ... I have URL encoded the date 2019-03-18
00:00:01 in the querystring. I can see that you comment shows a date
query string starting with %27 which is the URL encoded single quote.
You should only send the value in query string variables. Not the
single or double quote string delimiters. – asiby 18 hours ago
I want to validate for a 24 hour format.
The below code accepts 1:05:24 which is wrong, as it should instead only accept 01:05:24
try
{
foreach ($arr as $key=>$item)
{
if (date('H:i:s', strtotime($item[1])))
{
} else {
throw new Exception('Invalid Time Format');
}
}
}
catch (Exception $e)
{
echo $exp = $e->getMessage();
}
The following use of preg_match will differentiate between the two cases you have mentioned.
However, note that neither this nor the method that you mentioned in the question will correctly detect an invalid time such as 00:99:99.
If you require that, you need a different method, the easiest of which is probably to parse out the numbers and run this function on it.
<?php
$mydate_bad = "1:05:70";
$mydate_good = "01:05:24";
print (preg_match("/^\d\d:\d\d:\d\d$/", $mydate_bad)); # Returns 0
print (preg_match("/^\d\d:\d\d:\d\d$/", $mydate_good)); # Returns 1
?>
Based on the code you've provided, one way would be the following:
$php_date = date('H:i:s', strtotime($item[1]));
if ($php_date && $php_date == $item[1]) {
// valid date
}
This will check that a date could be created as in your code, and it will also ensure that the resulting date in the format H:i:s corresponds to what the user entered.
However, in terms of user-friendliness, if you can create a date from the user input, it might be better just to accept it and add the leading 0 yourself if it is missing. Simply use $php_date in favor of $item[1] afterwards.
Hey,
my question is based on that Topic:
Read excel xlsx file using simplexlsx in php
is there a possibility to catch that Date Format problem within the simplexlsx.class?
cause im using this script for a dynamic xlsx to database script and i hardly can say where i use the Date format or better wich cell has a date format.
So how can i use the unixstamp($excelDateTime) for unknown column within a while or for loop?
function value( $cell ) {
// Determine data type
$dataType = (string) $cell['t'];
switch ($dataType) {
thats the start of the value class... is it possible to add an switch case for datatype "Date"? if yes how?
example:
i got 3 Tables:
1: ID, col1, col2, col3
2: ID, col1, date, col3
3: ID, col1, col2, date
I have an < SELECT > to choose wich one is needed, but ill pass over the type of column, i want to display one of them with the correct Date not the "41928" UNIX-DATE?
Thx so far
regards
The only way to identify a date/time value in a cell is to look at the number format mask.
Function taken almost exactly from PHPExcel:
function isDateTimeFormatCode($pFormatCode = '') {
// General contains an epoch letter 'e', so we trap for it explicitly here
if (strtolower($pFormatCode) === 'general')
return FALSE;
// Typically number, currency or accounting (or occasionally fraction) formats
if ((substr($pFormatCode,0,1) == '_') || (substr($pFormatCode,0,2) == '0 ')) {
return FALSE;
}
// Try checking for any of the date formatting characters that don't appear within square braces
if (preg_match('/(^|\])[^\[]*[eymdHs]/i',$pFormatCode)) {
// We might also have a format mask containing quoted strings...
// we don't want to test for any of our characters within the quoted blocks
if (strpos($pFormatCode,'"') !== FALSE) {
$segMatcher = FALSE;
foreach(explode('"',$pFormatCode) as $subVal) {
// Only test in alternate array entries (the non-quoted blocks)
if (($segMatcher = !$segMatcher) &&
(preg_match('/(^|\])[^\[]*[eymdHs]/i',$subVal))) {
return TRUE;
}
}
return FALSE;
}
return TRUE;
}
// No date...
return FALSE;
}
I need to store data within a database, when I get the data from the database I need functions and variables in the string to be worked out as such.
Example
$str = "<p>Dear {$this->name},</p>"
I then store this in the database, and when I retrieve the string and run it through
eval("\$detail= \"$detail\";");
then the variable gets populated with the name. This is exactly what I needed and works fine.
The problem is I want to run a function with this variable as the parameter.
example. I would like to ucwords the variable.
I have tried:
$str = "<p>Dear {ucwords($this->name)},</p>" //just echoed {ucword(->name)},
$str = "<p>Dear {ucwords($this->name)},</p>" //Fatal error: Function name must be a string,
Am I going in the right direction?
Is this at all possible?
You don't need to keep PHP code in database. This is a bad practice and also can lead to security vulnerabilities.
Instead store in database string like this:
<p>Dear [name],</p>
And when you retrieve it you can just do:
$stringFromDb = str_replace("[name]", $this->name, $stringFromDb);
or
$stringFromDb = str_replace("[name]", ucwords($this->name), $stringFromDb);
Other common approach is to use sprintf. So you need to store in database string with %s as placeholders for values.
Example:
<p>Dear %s,</p>
and replace with
$stringFromDb = sprintf($stringFromDb, ucwords($this->name));
What you seem to be looking for is a simple templating language.
It's been a long while since I've written PHP (and I suddenly remember why...), but here's something I whipped up.
It should support both objects ($a->name) and arrays ($a["name"]) as input objects.
You can add new filters (name -> function name mapping) in $valid_filters.
$valid_filters = array("title" => "ucfirst", "upper" => "strtoupper");
function _apply_template_helper($match) {
global $_apply_template_data, $valid_filters;
$var = $match[1];
$filter = $valid_filters[trim($match[2], ':')];
$value = is_array($_apply_template_data) ? $_apply_template_data[$var] : $_apply_template_data->$var;
if($filter && !empty($value)) $value = call_user_func($filter, $value);
return !empty($value) ? $value : $match[0];
}
function apply_template($template, $data) {
global $_apply_template_data;
$_apply_template_data = $data;
$result = preg_replace_callback('/\{\{(.+?)(:.+?)?\}\}/', "_apply_template_helper", $template);
$_apply_template_data = null;
return $result;
}
How to use it:
$template = "Hello {{name:title}}, you have been selected to win {{amount}}, {{salutation:upper}}";
echo apply_template($template, array("name"=>"john", "amount" => '$500,000', "salutation" => "congratulations"));
The result:
Hello John, you have been selected to win $500,000, CONGRATULATIONS
I have found the following works,
If i contain the function within the class itself then it can be called using the following code
<p>Dear {\$this->properCase(\$this->rl_account->name)},</p>
But i would like to be able to do this now without having the database have the code as Alex Amiryan mentions earlier.
I am currently designing a website and database solution which allows an administrator to login and view/add/edit/delete data held in the database.
What is the best way to go about form validation for details the person will enter into these forms;
i.e. dates must have the convention xx/xx/xx
There are many different ways of validating such data but the most popular one is using regular expressions. You probably also want to check other things after validating the data format.
For instance if you want to check your date you can do the following:
function checkDateFormat($date)
{
//match the format of the date
if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts)) {
//check weather the date is valid of not
if(checkdate($parts[2],$parts[3],$parts[1]))
return true;
else
return false;
} else {
return false;
}
}
This will not only check the format of the date (in this case yyyy/mm/dd but you can modify it slightly to support yy/mm/dd or whichever order you want) but also will check if it is a valid date (e.g. 2001/02/31 is not a valid date).
You can do this native or by libraries like.
Native:
//If form was submitted
if ($_POST['submitted']==1) {
// check date format, ...
if ($_POST[date]){
$date = $_POST[date]; //If date was entered
}
...
}
Or by library: http://pear.php.net/package/HTML_QuickForm2
<?php
require_once 'HTML/QuickForm2.php';
require_once 'HTML/QuickForm2/Rule/Required.php';
require_once 'HTML/QuickForm2/Rule/Regex.php';
$form = new HTML_QuickForm2('tutorial');
$username = $form->addElement('text', 'username');
$form->addElement('submit', null, array('value' => 'Send!'));
$username->addRule(new HTML_QuickForm2_Rule_Required(
$username, 'Username is required!'
));
....