I'm unable to get the TIME for datetime datatype. I do get date but not able to get time and insert into database using PDO PHP. What needs to be done in the following code to get the time along with date.
I tried with H:i:s but it inserts data as 1970-01-01 00:00:00
Date format for input is 08/31/2020 - 05:04 pm
I am looking for date and time(hours and minutes).
$dp = date_create_from_format('m/d/Y',$_POST['dp']);
//CHECKS VALUE
$stmt->bindValue(':dp', $dp->format('Y-m-d'), PDO::PARAM_STR);
//GETTING POST VALUES
$dp=$_POST['dp'];
$dp = date('Y-m-d', strtotime($dp));
//BINDING PARAMETERS
$query->bindParam(':dp', date('Y-m-d', strtotime($dp)));
NOTE: All the above code works fine with getting and inserting date but does not work with date and time both.
There are two problems:
Your input string is in an unusual format. Therefore you need to use the DateTime::createFromFormat method to parse it (strtotime() can't do the job) using the correct format string.
Your output format is missing the time component - you need to add hours (in 24hr format), minutes and seconds to the string.
Here's a working example:
$dp = DateTime::createFromFormat("m/d/Y - H:i a", $_POST["dp"]);
$dpstr = $dp->format('Y-m-d H:i:s');
$query->bindParam(':dp', $dpstr);
Assuming $_POST["dp"] contains "08/31/2020 - 05:04 pm" then $dpstr will be 2020-08-31 17:04:00.
Demo: http://sandbox.onlinephpfunctions.com/code/b4cc0988c4eee60061502d86f38eccfc97aa9a49
Related
I have the following snippet of code:
date_default_timezone_set('UTC');
if (!isset($_POST['secret']) && $post_msg != "" ) { // checkbox unchecked processing...
// Checkbox is selected
$date_of_msg= date('l jS F Y h:i');
$msg_sent_by = $username;
$insert_query = "INSERT INTO user_thoughts VALUES ('','$post_msg','$date_of_msg','','' ,'$attach_name','$msg_sent_by','yes')";
$run_query = mysqli_query($connect, $insert_query) or die(mysqli_error());
}
When I echo $date_of_msg, the date and time will print out as expected, but when the following INSERT query above is ran, the field in the db will store 0000-00-00 00:00:00.
The field which will store $date_of_msg is called post_details and is of type datetime. I am aware that there is a function called date_to_str and have seem questions related to it, such as this one. But the answers in that question are converting manually inputted dates, whereas I want to get the time when a user makes a post. I think the solution is to use the date_to_str function when inserting the $date_of_msg variable? But I am unable to understand how it works?
Your
date('l jS F Y h:i');
is sending a result like:
Wednesday 2nd March 2016 10:00
so you are getting that value 0000-00-00 00:00:00 as a mismatch of datetime
Change it to:
date('Y-m-d h:i:s');
or format the date to something you need using this doc. http://php.net/manual/en/function.date.php
This is probably happening because your web server (apache im guessing) configuration is different than your mysql config.
So that's why you're experiencing some confusion.
There are a few ways to go about this, but it will require you to choose a format and run with it.
When inserting the record to mysql, tell it what format you want to use.
$query_manual = "INSERT INTO dateplayground (dp_name, dp_datetime)
VALUES ('DATETIME: Manual DateTime', '1776-7-4 04:13:54')";
Research mysql and apache date time configurations and fix both of them so they have the same exact date time format.
In mysql datetime field, you can insert only the date time of the format Y:m:d H:i:s
Please use the datetime with the same format
$date_of_msg = date('Y:m:d H:i:s');
i have this code but when i run it the equal value is did not show up only the greater than value was shown. example in my time i have 09-26-2015 in my database is also 09-26-2015 but how come it did not show up ?
$new_time = date("m-d-Y H:i:s", strtotime('-9 hours'));
$kuwery=mysqli_query($link,"select * from caritas_request_service
where hospital= '".$hsptal."' and status='".$status."' and doctor='".$name."'
and date >= '".$new_time."'");
You cannot use this data format in mysql. It is using Y-m-d H:i:s, and on the purpose. One can easily compare dates in this format, while trying to compare your format one will end up in a total mess.
Just change date field format to datetime, fix already existing data, and make your code
$new_time = date("Y-m-d H:i:s", strtotime('-9 hours'));
You have to understand that you don't have to store your data in the same format it gonna be used. Data have to be stored in the proper format. And then reformatted, if needed, at show time
There is a table column with the date format varchar and 0000-00-00 00:00:00.
Now I would like to display the date as a string. At the moment there is the date and even the time. I have no clue how to solve this.
I already tried converting by using strtotime():
$doa = $data['date']
...
$time = strtotime( $doa );
$date_format = date( 'd.m.y', $time );
what displays a wrong date 01.01.70.
Thanks in advance.
UPDATE:
...and here is how it will be stored in the db:
$timezone = 'Europe/Berlin';
date_default_timezone_set($timezone);
$timestamp = time();
$db_date = date("d.m.Y - H:i:ss", $timestamp);
When you're saving your date to the database, you're saving it as "varchar", so it's a string. Most probably your format is created like this before inserting the code into the database:
$date_format = date('Y-m-d H-i-s', $time);
This will convert a date to your "0000-00-00 00:00:00" format. I don't know about "months" and "days" at your format, so you might switch "m-d" pair in the above date() arguments.
Then you're pulling the data from the database, and in fact your have the correct code. You pull data in "0000-00-00 00:00:00" format, then convert it to Unix time (strtotime) and then format it using date() function. You just need to keep in mind that Unix time started at the 1th of January, 1970. This is why you're getting "01.01.70" from your code when applying zeros as a date string.
A short conclusion to all this: if you have any actual date (not all zeros) stored in your database, then all should be really fine.
Honestly speaking, PHP Date documentation has all clear details on date formatting, you can easily grab the idea.
Of course i can be mistaken and you have some more details, i just can't yet post to comments, a newbie here.
So im trying to insert a time using an input text field into a database table with data type TIME.
The format of time that I want to insert should be like this:
H:MM pm// example: 6:30 pm
The problem is the am/pm doesnt insert in my database table.
Only the hour and minute.
Please give me idea how to solve this.
Better with sample codes. Thanks.
Data Type TIME is for storing time data type - that means no AM/PM. Store the data in Your database in 24 hour format and format it to 12 hour format with am/pm in PHP or MySQL using one of these:
PHP:
$date = new DateTime($mysql_column['time']);
$date->format('h:i:s a');
or:
$date = date('h:i:s a', strtotime($mysql_column['time']));
or MySQL:
SELECT DATE_FORMAT('%h:%i:%s %p', time) FROM table;
Store the TIME as a standard format (18:30:00), and the format it however you want when you display it (Using DateTime objects or the date functions).
MySQL doesn't support extra formats when storing time data.
I think you want to add the jquery time picker value in your database with actual format in the database.
Here I have written some function
function update_time($time){
$ap = $time[5].$time[6];
$ttt = explode(":", $time);
$th = $ttt['0'];
$tm = $ttt['1'];
if($ap=='pm' || $ap=='PM'){
$th+=12;
if($th==24){
$th = 12;
}
}
if($ap=='am' || $ap=='AM'){
if($th==12){
$th = '00';
}
}
$newtime = $th.":".$tm[0].$tm[1];
return $newtime;
}
$time = update_time($_POST['time']); //here I am calling the function now you can insert the value in db
you just have to call the function and insert the returned value in database.
And while printing that you can do something like that echo date("h:i A",strtotime($time));
Change the type of the field to a varchar. TIME cannot store it like that. However, keep in mind that storing it like you want to will make it more difficult to provide localized results if that is something you will eventually need. That is, timezone support becomes difficult if you are not storing the timestamp itself, but rather a user-friendly representation.
EDIT: Or, DATETIME works as well, as was pointed out in the comments above.
You can use the DateTime Object in PHP which has functions to create a time object from any format and also has a function to output a time in any format like so
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>
http://php.net/manual/en/datetime.createfromformat.php
You would be best changing the field type to 'VARCHAR (32)', and then writing the time with PHP.
Example: date('m/d/y g:i:sa');
Why do you want to store the am or pm anyhow? If you store the date/time as a unix epoch timestamp, you can format the date however you want in the program - not the database.
Example: time(); - Store this in an INT(8) field.
date('m/d/y g:i:sa, $time()); - Output from DB like this.
try .ToShortTimeString() after your date variable.
I have been looking online for this answer and have come up empty...I am extremely tired so I thought I would give this a go....
I have a variable that has a date from a textbox
$effectiveDate=$_REQUEST['effectiveDate'];
What I am trying to do is take this date and add the current time
date('Y-m-d H:i:s', strtotime($effectiveDate))
When I echo this out I get 1969-12-31 19:00:00
Is this possible? Can someone point me in the right direction?
I found a solution to my problem....
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate . $currentTime));
echo $currentDate;
This takes a date from variable in one format and takes the date from another variable in another format and puts them together :)
Thanks everyone for their time.....
DateTime::createFromFormat
would also work but only if you have PHP 5.3 or higher...(I think)
The effectiveDate string is not in a format that strtotime recognizes, so strtotime returns false which is interpreted as 0 which causes the date to be displayed as January 1, 1970 at 00:00:00, minus your time zone offset.
The result you see is caused by the entered date not being in a format recognised by strtotime. The most likely case I can think of without knowing the format you used is that you used the US order of putting the month and day the wrong way around - this confuses strtotime, because if it accepts both then it can't distinguish February 3rd and March 2nd, so it has to reject US-formatted dates.
The most reliable format for strtotime is YYYY-MM-DD HH:ii:ss, as it is unambigous.
The date is just a timestamp, it is not object-oriented and i don't like it.
You can use the DateTime object.
The object-oriented best way is:
$effectiveDate=$_REQUEST['effectiveDate'];
// here you must pass the original format to pass your original string to a DateTimeObject
$dateTimeObject = DateTime::createFromFormat('Y-m-d H:i:s', $effectiveDate);
// here you must pass the desired format
echo $dateTimeObject->format('Y-m-d H:i:s');