could not check the datetime field value using PHP - php

I am facing one issue. In my MySQL datetime field some value is storing like 0000-00-00 and I need to check these value. I am explaining my code below.
if (strtotime($data['actualdateofaudit']) == 0) {
$data['actualdateofaudit']='';
}else{
$data['actualdateofaudit'] = date('d-m-Y', strtotime($data['actualdateofaudit']));
}
Here actualdateofaudit is my datetime field column name. In my case I could not check If there is any value like 0000-00-00 this. Here I need to check these type value.

Try This
if (strtotime($data['actualdateofaudit'])) {
$data['actualdateofaudit'] = date('d-m-Y', strtotime($data['actualdateofaudit']));
}else{
$data['actualdateofaudit']='';
}

Related

insert null value in date column in access when date is empty

I am in problem is that, i am using php ms access databse, when i am trying to update date in database its not updating, i want to update date field when user put date and when date is null column value should be null, my code following
if($cDate==NULL){
$cDate='Null';
}
else {
$cDate='02/03/2016';
}
if($buttonName=="Update"){
$sql_update="UPDATE 0D1_INDEX set C_date=$cDate where DN_='$mdn'";
and when date inserted it become time
Try This,
Set Default Value to NULL in Database
If I understand your question correctly you could use the isset() function to determine if the value of $cDate is null.
So you would have to do something like so:
if(!isset($cDate)){
$cDate = "Null";
} else{
$cDate='02/03/2016';
}
isset() determines if the value is not NULL so putting an ! in front of it means that if the value is NULL

Date value is not inserting or updating in MySQL Database Prestashop

When I update my date field in MySql database Prestashop It's not inserting in the table where column type is "Date".
And when I change the column type to "varchar" , It's calculating the date means If date comes in this format 2016-4-2 then value will be stored "2010". And when I place date manually in the query then it works. Please help me out.
if (Tools::getValue('id_abono')) {
print_r($_POST['datetex']);
$dtt= $_POST['datetex'];
$dtr= date("Y-m-d H:i:s", $dtt);
Db::getInstance()->execute('Update '._DB_PREFIX_.'lgabonos set date='.$dtr.'
WHERE id_abonos = '.pSQL(Tools::getValue('id_abono')).'');
$output .= #Module::displayConfirmation($this->l('The credit note has been successfully updated'));
}
Try this query :
Db::getInstance()->execute('Update '._DB_PREFIX_.'lgabonos set date="'.$dtr.'"
WHERE id_abonos = '.pSQL(Tools::getValue('id_abono')).'');
I think it will solve your problem

MySQL - Compare all values in a column and write in another column

I need to create a script that compares one field in the database (has a date stored, it's type is "TEXT" and cannot be changed DATE) to the current server date.
The dates are encoded like this "1380571547", so i need to use strftime() to decode them. This field for example, decoded with strftime corresponds to this "Sep-30-2013, 22:05"
What I need is to compare those fields with the current date, and according to that condition, write something like "Expired" in another field.
To achieve this, I made this block of code:
<?php
require("connection.php");
$today = strftime('%b-%d-%Y, %H:%M');
$exp_date = mysql_query("SELECT numbers FROM date");
while($row = mysql_fetch_array($exp_date))
{
echo (strftime ( '%b-%d-%Y, %H:%M', $row ['numbers'])). "<br />";
}
if ($exp_date < $today) {
$sql = "INSERT INTO date (changed) VALUES ('EXPIRED')";
$result = mysql_query($sql);
echo "ADDED!";
}
?>
However, this code is not working, can someone help me ?
PHP is not my strong point but it looks to me like you condition is doing a comparison on an array,
IE:
if ($exp_date < $today) // will always be false.
Your code would probably have to look something more like this.
while($row = mysql_fetch_array($exp_date))
{
if ($row[0] < $today)
{
$sql = "Update date set changed = VALUE where rowid = rowid";
$result = mysql_query($sql);
echo "ADDED!";
}
}
having said that i would probably do the comparison and update in SQL using a case statement,
Update Date
set changed = case when number > ExpiryDate
then "Expired"
else "Current"
end
You can do all this in a single query:
UPDATE `date` set `changed`='Expired' where date(now()) > date(from_unixtime(`numbers`))
But this is not what your code is attempting to do. Your second block seems to be inserting the word Expired in new rows, rather than updating anything.
Note that the table name date should be wrapped in backticks to avoid any possible clash with MySQL keywords
I don't understand the second block of code with the insert. I would do an update inside the loop. but if your going to do that, it could probably be done in one combined update statement.

Sending date and time form fields to MySQL with PHP

I've searched through the posts for an answer and the closest answer came from the topic "trying-to-post-date-via-php-with-mysql-need-help"
I've got a form with 2 input fields and .The user selects the date from an html5 popup calendar picker, types in the time, and submits the form to a mysql db. I'm having trouble getting the date and time fields to validate/upload to the db.
In my MySQL db I have a table called appointments with a column called curdate that has a type of DATE; another column is called curtime with a type of TIME.
My PHP file has the PHP validation and SQL actions at the top of the file and the sticky html form at the bottom. The form uses to send the form
Here is the code:
$td = date( 'Y-m-d H:i:s', $phpdate );
$phpdate = strtotime( $td );
if(isset($_POST['submitted'])) {
require_once('mysqli_connect.php');
$errors=array();
if(empty($_POST['curdate'])) {
$errors[]='Your date did not match';
} else {
$td=mysqli_real_escape_string($dbc,trim($_POST['curdate']));
}
if(empty($_POST['curtime'])) {
$errors[]='Your time is empty';
} else {
$tt=$_POST['curtime'];
}
if(empty($errors)) {
$q="INSERT INTO users (first_name,last_name,email,curdate,curtime,physician,reason) VALUES ('$fn','$ln','$e','$td','$tt','$phys','$reason')";
$r=#mysqli_query($dbc,$q);
if($r) {
echo '<h1>Thanks</h1>
<p>You are now registered</p><p><br/></p>';
} else {
echo '<h1>System Error</h1>
<p class="error">You could not be registered</p>';
echo '<p>'.mysqli_error($dbc).'<br/><br/>Query:'.$q.'</p>';
}
mysqli_close();
include('includes/footer.html');
exit();
} else {
echo '<h1>Error</h1>
<p class="error">The following errors occurred:<br/>';
foreach ($errors as $msg) {
echo "-$msg<br/>\n";
}
echo '</p><p> Please try again</p><p><br/></p>';
}
mysqli_close($dbc);
}
Mysql Or Mysqli Supports date format as 'yyyy-mm-dd' so u need to convert date and time in proper format before inserting in DB
If you want to insert current date and time when the user submits the form then Use following Query
INSERT INTO users (first_name,last_name,email,curdate,curtime,physician,reason)
VALUES ('$fn','$ln','$e',CURDATE(),CURTIME(),'$phys','$reason')
But if you want to insert date from form field then do following
$td=$_POST['curdate'];
$tt=$_POST['curtime'];
$td=date('Y-m-d',strtotime($td)); //Converts date to 'yyyy-mm-dd' acceptable to mysql
$tt=date('H:i:s',strtotime($tt)); //converts time to HH:mm:ss
//Now apply Your Query as
INSERT INTO users (first_name,last_name,email,curdate,curtime,physician,reason)
VALUES ('$fn','$ln','$e','$td','$tt','$phys','$reason')
You might have a problem, if you in mysql made that row in table dateandtime type, if that's the case you won't be able to store it like that. If you want it to be like that just use text or varchar ....
DATE - Stores a date value in the form YYYY-MM-DD. For example 2008-10-23.
DATETIME - Stores a date and time value of the form YYYY-MM-DD HH:MM:SS. For example 2008-10-23 10:37:22. The supported range of dates and times is 1000-01-01 00:00:00 all the way through to 9999-12-31 23:59:59
TIMESTAMP - Similar to DATETIME with some differences depending on the version of MySQL and the mode in which the server is running.

Test date value against null value

I like to compare a date (extract from database with a null value (0000-00-00).
I try to do like that:
$dateValue = $row_recordset3['ttDateEnvoiDEP'];
if ($dateValue === '0000-00-00') {
$dateEnvoiDEP = "CURRENT_DATE()";
}
else {
$returnOK .= $dateValue;
$dateEnvoiDEP = $row_recordset3['ttDateEnvoiDEP'];
}
But it doesn't work, do you know why?
why don't you do it directly in your SQL query, something like this,
SELECT IF(ttDateEnvoiDEP = '0000-00-00', CURDATE(), ttDateEnvoiDEP)
as `ttDateEnvoiDEP`,
FROM ...
WHERE ...
CURRENT_DATE() is a MySQL function, not PHP.
Give this a read: http://php.net/date
Also, if you want to use the current date in that format you can do date('Y-m-d') which will return 2012-09-03 or whatever the date is.
If you want to deal with date with PHP you can use unix time stamp for that.
Read php time and date for more information
The problem is your code is you are compareiring 0000-00-00 to $dateValue.0000-00-00 is not null. Not clear what you what here. But I think you want to know if the value is set. so you can try
isset($dateValue)
Hope this could help you!
EDIT:
if(!isset($dateValue)){
$dateEnvoiDEP = time();
}else{
//do other stuff
}

Categories