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
Related
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
I have created the text box as well as the query for database.But in my case I want to fetch the data from input field and store it in database.
What actually happens is when I use the function $_POST['launch_date'] it displays the date that I add in db but when I store this in a variable and convert it to y-m-d format it doesn't give me the answer from the text field like $launch_date=date("Y-m-d",strtotime($_POST['launch_date']));.
when i print the both the above mentioned code in single line like echo $_POST['launch_date'] .$launch_date."<br>"; I get the following results
30/01/2020 1970-01-01.
The first one is from my text box and the second one is from the variable that I have created.
Use DateTime::create_from_format() to specify the format of your dates.
$launch_date = DateTime::create_from_format('m/d/Y', $_POST['launch_date'])->format('Y-m-d');
$_POST['launch_date'] = '30/01/2020';
$date = str_replace('/', '-', $_POST['launch_date']);
$launch_date = date('Y-m-d', strtotime($date));
echo $_POST['launch_date'] ." ".$launch_date."<br>";
Output: 30/01/2020 2020-01-30
The issue here is that the / has confused the format of m/d/Y (instead of d/m/Y). And if the date time is not valid (for instance, the month is greater that 12) is in the American format, you'll get the default time (aka UNIX timestamp 0) ie 1970-1-1.
You can create date from user input by
$launch_date = DateTime::create_from_format('d/m/Y', $_POST['launch_date'])->format('Y-m-d');
This is create a date variable of the form Year-Month-Date.
You can change the format as per your requirement.
Y-m-d is the standard date format for MySql.
If You are using PHP version 5.2 or lower you , you will have to parse the date in to 'd'. 'm' and 'y' . Then you will have to create a new date.
Date and time from URL shall be in format YYYY-MM-DD-HH-II-SS. I want to check it correctness and insert into database in column of mysql timestamp format.
1) With $timestamp = $this->input->get('time_from') I receive it from url.
2) Before insertion in database I try to print it.
if (! empty($timestamp)) {
$timestamp = date_create_from_format('Y-m-d-H-i-s', $timestamp);
if (! $timestamp)
echo date_format($timestamp, 'Y-m-d H:i:s');
}
3) I obtain that year could be before 1970, for example 1950-01-01-10-32-01 is correct by this checking. But it is impossible to insert such date in mysql column.
4) I obtain that 2012-02-31-10-00-00 is transformed automatically in 2012-03-02 10:00:00.
The questions are: how to avoid these confusing automatic transformations? What is the best way to provide such checking?
You cannot store a date before 1970 in a MySQL TIMESTAMP column. Use DATETIME instead. http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html
You can use the checkdate() function to see if a date is valid.
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.
Currently I store the time in my database like so: 2010-05-17 19:13:37
However, I need to compare two times, and I feel it would be easier to do if it were a unix timestamp such as 1274119041. (These two times are different)
So how could I convert the timestamp to unix timestamp? Is there a simple php function for it?
You're looking for strtotime()
You want strtotime:
print strtotime('2010-05-17 19:13:37'); // => 1274123617
Getting a unixtimestamp:
$unixTimestamp = time();
Converting to mysql datetime format:
$mysqlTimestamp = date("Y-m-d H:i:s", $unixTimestamp);
Getting some mysql timestamp:
$mysqlTimestamp = '2013-01-10 12:13:37';
Converting it to a unixtimestamp:
$unixTimestamp = strtotime('2010-05-17 19:13:37');
...comparing it with one or a range of times, to see if the user entered a realistic time:
if($unixTimestamp > strtotime("1999-12-15") && $unixTimestamp < strtotime("2025-12-15"))
{...}
Unix timestamps are safer too. You can do the following to check if a url passed variable is valid, before checking (for example) the previous range check:
if(ctype_digit($_GET["UpdateTimestamp"]))
{...}
If you're using MySQL as your database, it can return date fields as unix timestamps with UNIX_TIMESTAMP:
SELECT UNIX_TIMESTAMP(my_datetime_field)
You can also do it on the PHP side with strtotime:
strtotime('2010-05-17 19:13:37');
if you store the time in the database, why don't you let the database also give you the unix timestamp of it? see UNIX_TIMESTAMP(date), eg.
SELECT UNIX_TIMESTAMP(date) ...;
databases can also do date and time comparisons and arithmetic.