I have a small problem. I'm using PHP with Oracle (new to the Oracle by the way).
In my database there's a DATE field called NEXT_START_DATE and it's value is
25.12.2013 04:05:01
as you can see below.
The thing is I can get date values just fine in my web page, but couldn't see anything like a time, if you can see below image, it only returns 25/12/2013.
I know that people suggested to use pl/sql functions like
to_date()
or
to_char()
but is this possbile using just php? I really can't interfere the SQL. Any help would be awesome, Thanks in advance.
In ADOdb, make sure to set the format before connecting:
$db = ADONewConnection("oci8");
// $db->debug = true;
// Date format is set before connecting.
$db->NLS_DATE_FORMAT = 'DD.MM.YYYY HH24:MI:SS';
Courtesy: http://board.issociate.de/thread/192412/OCI_ignoring_NLS_DATE_FORMAT_parameter.html
You can use SQL functions in your queries as well.
For example,
SELECT TO_CHAR(next_start_date, 'YYYY-MM-DD HH24:MI:SS') FROM mytable
and you'll get the date in the format you specified.
As #Maheswaran Ravisankar pointed out, there is NLS_DATE_FORMAT as well, but if you set it, that format is used for all queries (that do not specify to_char). I always use to_char in all my selects, because it allows me to specify an individual format for each query.
Related
I am using the p4a application framework and I have build several databases one of which needs to gather the date of a booking, I understand that there Isn't a way to do this through MySQL but I haven't found anything useful on the p4a forums on this so anyone that uses the p4a framework that could help, I would be grateful,
I have my local set as en_GB which sets the date within the p4a field as dd-mm-yyyy but I need it to be yyyy-mm-dd to actually write the data into the database,
the present code for this operation is:
$this->build("p4a_field","date")
->setlabel("Date")
->setType('date')
$location = $this->AreaName->getNewValue();
$date = $this->date->getNewValue();
$merono = $this->merono->getNewValue();
$p4a = p4a::singleton();
$p4a->i18n->autoUnformat($date, "shortdate");
p4a_db::singleton()->query("INSERT INTO meetingrooms(location, date, merono)
VALUES
('$location', '$date', '$merono')");
Any help would be appreciated, I was planning on intercepting the function using afterClick but I need to know the syntax required first.
Thanks,
Steve
If I understand correctly, you have some dates in a non-MySQL-friendly format, but you want to use them for INSERTs, right?
How about letting MySQL convert them for you?
SELECT STR_TO_DATE('31-12-2012','%d-%m-%Y');
-> '2012-12-31'
EDITS:
It looks like you have the date:
$date = $this->date->getNewValue();
so you need to use the formula in your SQL:
p4a_db::singleton()->query("INSERT INTO meetingrooms(location, date, merono)
VALUES
('$location', STR_TO_DATE('$date','%d-%m-%Y'), '$merono')");
I'm not a p4a guy, so hopefully that'll work.
Note that, in most languages, you'll be exposed to SQL injection with code like that. Does p4a cover that for you, or provide for positional parameters?
Good luck.
It seems like there are too many complicated ways of doing this, so I'm looking for a clean, succinct answer to this issue.
I write a blog, I click submit, and the title, content, and timestamp INSERTS INTO my blog table. Later, the blog is displayed on the blogindex.php page with the date formatted as MM-DD-YYYY.
So this is my 3 step question:
What is the best column type to insert the date into? (ex: INT, VARCHAR, etc)
What is the best INSERT INTO command to use? (ex: NOW(), CURDATE(), etc)
When I query the table and retrieve this data in an array, what is the best way to echo it?
I'm new at PHP/MySQL, so forgive me if I don't know the lingo and am too frustrated reading 1000 differing opinions of this topic that do not address my issue specifically, or only cover one of the 3 questions...
Here is my opinion on your three questions:
Use the correct data type: Date or DateTime. I would choose for the DateTime type as you store the time as well (might be very handy if you want to have some kind of order, when you added the posts).
It all depends whether you just want the Date (use CURDATE()) or the Date + Time (use NOW()).
You fetch the data and format it how you want it. Don't format it yet in the query, just use the correct PHP functions for it (for example with DateTime). How you fetch the data, doesn't matter too much; you can use PDO or MySQLi or ...
Always store and process dates and times in UTC and perform timezone adjustments in your presentation layer - it considerably simplifies things in the long-term.
MySQL provides a number of different types for working with dates and times, but the only one you need to worry about is DATETIME (the DATE type does not store time information, which messes up time zone conversion as information is lost, and the TIMESTAMP type performs automatic UTC conversion (which can mess up programs if the system time zone information is changed) and has a smaller range (1970-2038).
The CURDATE() function returns only the current date and excludes time information, however this returns information in the local timezone, which can change. Avoid this. The NOW() function is an improvement, but again, returns data in the current time zone.
Because you'll want to keep everything in UTC you'll actually want to use the UTC_TIMESTAMP function.
To return the value you'll need to execute SQL commands in sequence with variables, like so:
SET #now = UTC_TIMESTAMP()
INSERT INTO myTable ( utcDateTimeCreatedOrSomething ) VALUES ( #now )
SELECT #now
Date would probably be the best type, although datetime will work as record more accurate as well.
There isn't a 'best insert into', but what do you really want and how accurate you want the date to be. For a blog, I would say make it datetime and use NOW(). so visitors can see quite accurate of when this post is made.
surely you can easily find huge to run sql and fetch a select query from sql using php by google, so I'll leave this easy work to your self.
For echo the date, you can use the php date format such as:
$today = date("m-d-y"); // 03-10-01
I think Styxxy has it pretty well right, but here is a links for your PHP date formatting part...
How to format datetime most easily in PHP?
(Supporting link: http://php.net/manual/en/datetime.format.php )
Basically it's
echo date("d/m/Y", strtotime('2009-12-09 13:32:15'))
... although, I think the strtotime is unnecessary as it should already have the type of datetime.
In terms of the MySQL, yes, do it as a datetime col, use NOW() as the SQL keyword, and depending on how you want to get it from the database you could...
SELECT CAST(col_name AS DATE) .... or .... SELECT CAST(col_name AS DATETIME) <-- this last one is implied due to the col type.
good luck! :)
I am creating a mysql db with a php frontend. The data it will use is extracted from another larger db and contains a date/time field which looks like this - 20120301073136 - which records when an event happened.
I understand that this might be a UNIX timestamp? Not sure.
I want to be show this field in the tables in my PHP webpage as a readable date and time -
ie something like 01-Mar-2012 07:31:36 or similar
Should I try and convert it with SQL command or let PHP format it? And, what is the code to do so?
BTW, it is important that I can sort the data (in SQL and in the PHP table) into date order - ie in the order that these events happened.
Thanks in advance for your help - Ive learnt a lot here already
J
You can convert it to a datetime directly in your SQL query. Example:
select cast(20120301073136 as datetime)
You can also order that with no need to convert it since it is a number in the format YYYYMMDDHHmmss
select * from yourTable
order by yourDateTimeField
You should make use of the MYSQL DATE functions. Check the docs before asking simple questions. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html.
Also you can sort the dates directly in your query using ORDER BY.
Is it possible to change the current 24 hr format like 22:16:08 to something like 10:16:08 in mysql?
I want to change how these values are saved. Is it possible or I will just let php handle things for me?
Don't change the way how these values are saved. Change the way they are output.
See TIME_FORMAT()
In your case, this would show a TIME field as 10:16:08 PM:
SELECT TIME_FORMAT(timefield, "%l:%i:%s %p") AS date_formatted;
You should always store date/time values in their native format, which in MySQL is the 24hr format. You can change how they're retrieved with
SELECT DATE_FORMAT(somedatetimefield, 'format string here')
FROM table
where the format string options are defined here
If doing that manually for every query is a problem, you can always create a view to do it for you automatically.
You really shouldn't change how the data is saved in MySQL. Instead you should only present it differently. You can use PHP's date function to format the date in anyway you want. This is a huge advantage because you are separating how the data is saved and how the data is presented.
Using DATEFORMAT you can save/fetch the date how you wish (much like PHP date()'s syntax)
Like Pekka said: Don't change the way how these values are saved.
You can also easily handle this in php using date()
like date("hh:ii:ss")
This puts out the format you used in your example. (which is 12-hours format and leading zeros.)
I have several db calls in my site with bind_variables that works fine. But, I can find the correct sign for Date in the documentation, for the command:
$query->bind_param("ssi",...);
I don't want to do something like:
$db->query('SELECT item FROM table WHERE something='.$something);
Since this is string manipulation, not binding. (In binding the query is left with the "?" and that makes the queries faster because the DB sees them the same only with different cariables.)
If I wasn't very clear, I want to do the same as this only with a date variable type.
Some extra information to my comment given above:
If I do understand correctly have a
look at:
Using Mysqli bind_param with date and time columns?.
It looks like you can just treath it
as a string.
If you want to do it with bind_param it is the only way I know to do it and I don't see any problems. If mysql receives a wrong formatted date it will insert a 0000-00-00 value to your table.
Can you tell me what you think could be a problem? If you insert it as a normal query you also use the same syntax as a String.
For dates, you will have to format them before calling bind_param:
$query->bind_param('s', $date->format('Y-m-d H:i:s')); // assuming $date is a DateTime object
Dates should be bound as strings in a format MySQL accepts (yyyy-mm-dd).