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.
Related
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
I'm implementing the following in PHP with stores this string in the DB for the "created_at" column: Wed, 17 Dec 2014 14:53:02 +1100.
$created_at = date('r', $insta['created_time']);
Now I'd only like to do the insert if $created_at is more than a certain date, for example:
if ($created_at > "Wed, 15 Dec 2014 00:00:00 +1100") { //insert in to db }
That doesn't work though. I'd usually use strtotime() but unsure how to go about it when the data is set in that format. Also, the column type is varchar(255).
Aside from your question, you need to store datetime data in a datetime column. Mysql has specific functions and formatting for this reason. But, in order to store it will need to be in Y-m-d H:i:s format. Doing this will save you big hassles down the road.
In regards to your question, you can use PHP's DateTime class if you are using PHP version 5.2+.
$compareTime = new DateTime('2014-12-15 00:00:00'); //new datetime object//
$createdTime = new DateTime($created_at); //converts db into datetime object//
if ($createdTime > $comparedTime) { ..insert into DB.. }; //compare them//
Why are you storing WED in database..just store date with the php function date("Y-m-d H:i:s") and the table field type will be date..
This way mysql automatically give u facility to compare two dates. You can simply use if else statement to compare two dates.
for PHP version >= 5.3
Use date_create_from_format or DateTime::createFromFormat
Although I think strtotime would also work but to be sure pass date time format and value to above function. For detail of format see documentation.
Use strtotime().
$created_at = strtotime($insta['created_time']);
$checkdate = strtotime(date('Y-m-d H:i:s')); //here we have to insert check date
if($created_at > $checkdate){
}
In my PHP script I've got a function handling birthdays like so:
$dateTime = \DateTime::createFromFormat('U', $time);
The problem is that this returns false with negative $time numbers (i.e. dates before 1-1-1970). In the PHP docs there's a comment saying that indeed
Note that the U option does not support negative timestamps (before
1970). You have to use date for that.
I'm unsure of how to use Date to get the same result as DateTime::createFromFormat() gives though. Does anybody have a tip on how to do this?
If you just need to format a UNIX timestamp as a readable date, date is simple to use:
// make sure to date_default_timezome_set() the timezone you want to format it in
echo date('Y-m-d H:i:s', -12345);
If you want to create a DateTime instance from a negative UNIX timestamp, you can use this form of the regular constructor:
$datetime = new DateTime('#-12345');
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.