I want to insert two datetime's into mysql.
$taskcompdate is a user written string that needs to be converted into a datetime object and formatted with a "d/m/y" format.
The second, $datenow is simply the now time.
The insert query works, but all dates in all records are displayed as 0000-00-00 00:00:00
I tried every possible way I found, but none works. What am I doing wrong?
<?php
$conn = mysqli_connect("127.0.0.1", "root", "", "todo_list");
$taskname = $_POST["taskname"];
$taskcompdate = $_POST["taskcompdate"];
$datenow = date("H:i:s d/m/y");
$insert_date1 = date('d/m/y', strtotime($taskcompdate));
$insert_date2 = date('H:i:s d/m/y', strtotime($datenow));
$sql_main = "INSERT INTO task_main (task, complete_date, added_date) VALUES ('$taskname', '$insert_date1', '$insert_date2')";
$result = mysqli_query($conn, $sql_main);
if ($result) {
echo 'success';
} else {
echo 'failure' . mysqli_error($conn);
}
?>
Your dates are in an invalid format for MySQL's date and datetime types. They must be in YYYY-MM-DD HH:MM::SS format.
$insert_date1 = date_format($taskcompdate, "Y-m-d");
$insert_date2 = date('Y-m-d H:i:s', strtotime($datenow));
Related
After inserting data from pstgrsql database I need to convert timestamp to date.
I tried php date() function but I only got value - 01/01/1970. Here is example of my code:
$query = "SELECT * FROM \"user\" WHERE verified= 't'";
$result = pg_query($conn, $query);
while ($row = pg_fetch_row($result)) {
$date = date('d/m/Y', $row[11]);
echo "\n";
echo "$row[0] , $row[1] , $row[3], $date ";
}
$ReportRow = array('date' => $date, 'activereg' => $activerag);
$ReportRow1 = array('date' => $date);
$ReportRow2 = array('date' => $date);
$report = array($ReportRow, $ReportRow1, $ReportRow2);
*there will be more data stored in these arrays but its just work in progress for now. Thanks all for any kind responses :)
EDIT 1
DB has rows like
`3125, alex, alex#example.com, 01/01/1970`
You are using date() to try to convert a string into a date, when date only accepts a full timestamp.
date(format,timestamp);
Rather, you want to convert your string in your result into a time value. Try
$time = strtotime($row[11]);
and if you need to switch it about use
$newformat = date('Y-m-d',$time);
If you need a timestamp after that, you can
$timestamp = $time->getTimestamp(); // Unix timestamp
I have the following code and I'm able to add to today's date, but if the existing date is before today it still returns today's date. MySQL data is stored as a y-m-d string.
$date = date('Y-m-d');
$oldate = "SELECT date FROM signup WHERE user='$user'";
$result1 = mysqli_query($connection, $oldate) or die(mysqli_error($connection));
if (strtotime($result1) < $date) {
$date1 = date('Y-m-d', strtotime("+$months months", strtotime($date)));
} else {
$date1 = date('Y-m-d', strtotime("+$months months", strtotime($result1)));
}
You are comparing timestamp value ($result1) with string $date1.You need to cast your $date1 to timestamp as well. Change your if condition to make it work as follow:
if(strtotime($result1)<strtotime($date))
{
....
}
else
{
.....
}
The reason you're above code isn't working is because $result1 won't be a date. mysqli_query will return an instance of mysqli_result when you've performed a successful select query.
http://php.net/manual/en/mysqli.query.php :
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
You can use mysqli_fetch_assoc to get the information you want:
$row = mysqli_fetch_assoc($result1);
$rowDate = $row['date'];
Also, as Prada pointed out you can't not compare dates in the way you have in your question, however, you could do something like:
if (strtotime($rowDate) < strtotime($date))
Alternatively, you could use DateTime and have something like:
$oldate = "SELECT date FROM signup WHERE user='$user'";
$result1 = mysqli_query($connection, $oldate) or die(mysqli_error($connection));
$row = mysqli_fetch_assoc($result1);
$now = new DateTime;
$date = new DateTime($row['date']);
$date1 = $date < $now
? $now->modify("+$months months")->format('Y-m-d')
: $date->modify("+$months months")->format('Y-m-d');
Hope this helps!
I want to separate Date and time from retrieved timestamp in oracle with php but it's not working. I tried to separate date and working after that i have to separate time but i am getting empty timestamp.
$db = $row['CREATEDATE']; // CREATEDATE=03-SEP-15 12.55.17.000000 PM
echo 'db...'.(string)$db."<br/>";
$timestamp = strtotime((string)$db);
echo 'timestamp...'.$timestamp."<br/>";
echo date("m-d-Y", $timestamp);
Convert your received column data to a string, and convert it to a datetime. Then you can do all other formatting on it.
$db = $row['CREATEDATE']; //$db need to be converted to string type
$datetime = new DateTime((string)$db); //I expect (string)$db will considered as a string
$date = $datetime->format('Y M d'); //convert to date
$time = $datetime->format('H:i:s'); //convert to time
$select_stmt = "select to_char(ENDTIME,' hh24:mi:ss') AS CHENG,to_char(RESERVETIME,' hh24:mi:ss') AS CHENG2 FROM RESERVATION WHERE RESERVATIONSTATUS='RENTING' AND RESERVATIONFACILITY = '".$_SESSION['cno']."'";
$stid = oci_parse($con, $select_stmt);
oci_execute($stid);
while ($row = oci_fetch_assoc($stid))
{
$datt=$row["CHENG"];
$datt2=$row["CHENG2"];
}
Got stuck in a complex or maybe stupid problem. I am getting a query from mysql, and then trying to compare a date column with a PHP data which i formatted to the same format i.e "Y-m-d" it always results in no match, although i see there is a match.. and it gets the right result set too.
<?php
date_default_timezone_set('America/Los_Angeles'); // set timezone to our timezone
$constantTime = time(); // get value of time in constant
$appDate = date("Y-m-d", $constantTime); //that defines php time variable -
$queryDate = "SELECT * FROM date WHERE date='$appDate'";
$resultDate = mysql_query($queryDate) or die("Sorry Website Under Maintainence");
$recordDate = mysql_fetch_array($resulDate);
if ($appDate == date("Y-m-d", strtotime($recordDate['date']))) {
echo "MATCH ";
$dateID = $recordDate['dateID'];
} else {
mysql_query("insert into date(date) values('$appDate')")or die("Database write error1");
$resultDate = mysql_query($queryDate) or die("Sorry Website Under Maintainence");
$recordDate = mysql_fetch_array($resultDate);
echo "NO MATCH ";
$dateID = $recordDate['dateID'];
}
This is always triggering the else, i tried === instead of ==, i tried strcmp
As i assume you're comparing datetime field, you have two possibilities:
Cast field to date:
$queryDate = "SELECT * FROM your_table WHERE date(your_date_field) = date('$appDate')";
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date
or
Modify your date format to be ISO compatible:
$appDate = date("Y-m-d H:i:s", $constantTime); //it defines date in format 2015-03-14 15:00:00
$queryDate = "SELECT * FROM your_table WHERE your_date_field='$appDate'";
See also this question
I need to insert the datetime value entered from the HTML form using PHP into the MySQL database. However I receive the Incorrect datetime value error each time I try to execute the code below,
$rosterstartdate=$_GET['rosterstartdate'];
$rosterenddate=$_GET['rosterenddate'];
//$date = date_create_from_format('d/M/Y:H:i:s', $rosterstartdate);
//$date->getTimestamp();
//echo $date;
$date = strtotime($rosterstartdate);
echo date('d/M/Y H:i:s', $date);
// echo DATE_FORMAT($rosterstartdate,"%Y%m%d %H%i%s");
$con=mysql_connect("localhost","root","");
if($con==true){
mysql_select_db("attendance_db",$con);
$query="insert into tblroster values(LAST_INSERT_ID(),'$rosterteam','$rostershifts','$date','$rosterenddate')";
I have tried using each of the different techniques above to do the conversion but it does not work. Any ideas on how this could be inserted
try this:
$date = date('Y-m-d H:i:s', $date);
Instead of echoing it out, use that code to format the date.
However, it looks like what you really want is this:
$rosterstartdate = date('Y-m-d H:i:s', strtotime($rosterstartdate));
$rosterenddate = date('Y-m-d H:i:s', strtotime($rosterenddate));
This way you can just reference those two variables in your query string.
You don't need to format it if you have a unixtime use FROM_UNIXTIME,
change your query as
$rosterstartdate=$_GET['rosterstartdate'];
$rosterenddate=$_GET['rosterenddate'];
//$date = date_create_from_format('d/M/Y:H:i:s', $rosterstartdate);
//$date->getTimestamp();
//echo $date;
$date = strtotime($rosterstartdate);
$con=mysql_connect("localhost","root","");
if($con==true){
mysql_select_db("attendance_db",$con);
$query="insert into tblroster values(LAST_INSERT_ID(),'$rosterteam','$rostershifts',FROM_UNIXTIME($date),'$rosterenddate')";