Changing the date format for MySQL within a p4a field - php

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.

Related

Displaying date time with Php and Oracle. Time fails?

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.

Best format for storing dates in sql

I am trying to optimise how I use dates across my SQL/PHP site. I'm familiar with date() and strtotime() functions, and have created projects which have stored data on the server side in both unix and datetime. I know that changing date doesn't use massive amounts of resource, but I'm trying to understand how to code in the most efficient way.
What I'm trying to work out now is what the quickest/most efficient practise is for storing dates on a server, i.e: What is the most effective combination of SQL FROM_UNIXTIME, CONVERT, UNIX_TIMESTAMP and PHP strtotime and date functions, for a typical table involving frequent CRUD of the date fields. Take the following example:
I have an SQL table which contains 3 date columns. All 3 of these can be updated quite regularly by multiple users of the site, and entries are also entered in to it as follows:
mysql_query("INSERT INTO `regular` (
`regularid`,
`propertyid`,
`userid`,
`billdate`,
`billfrequency`,
`enddate`,
`amount`,
`description`,
`payee`,
`payer`,
`lastpayment`
)
VALUES (
'', '{$_POST['propertyid']}', '$varuserid', FROM_UNIXTIME($date), '{$_POST['billfrequency']}', 'FROM_UNIXTIME($edate)', '$amount', '{$_POST['description']}', '{$_POST['payee']}', '{$_POST['payer']}', 'FROM_UNIXTIME($ldate)'
)") or die(mysql_error);
Print "A regular payment for £".$_POST['amount']." has been created<br/>\n";
Entering in to the database I parse the date fields from a user entry form to a variable in mm / dd / yyyy format, validate it with the strtotime() or mktime() functions, and then submit it using from_unixtime.
However, then displaying information from the database I use:
date("d-m-Y", strtotime($val['billdate']))
That means I take a dd/mm/yyyy date format from the user and another one from the database. I then convert these to unixtime using strtotime, and then convert it back to British date format using date - This is surely doubling? if not tripling? the server query.
My question is in 2 parts.
Firstly, is it quickest a) storing all date formats in unixtime in sql and converting to date format through the server side query or b) storing all date formats in date format and converting to unixtime only when accessing them
(I thought the latter would be best, but my example above shows that even doing that I seem to be using more server time than I ought to)
Secondly, could you point me in the right direction for how I should carry out my own time benchmarks of scripts - I'm only really beginning to understand testing as I'm still relatively new to Php, but I'm keen to learn.
Thanks so much!
Use MySQL's "unix_timestamp()", it's short and can be converted to readable full date within a second by using php, bash etc.

Insert current date to MYSQL table, then echo back

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! :)

MYSQL DateTime vs strtotime string held in database

I am trying to work out which is the best solution for me - i am making a roster application and have been working with the string, but think it may be beneficial to switch to the datetime type - currently working with dates is getting hard, trying to put the shifts onto a roster without doing a query for every single cell.
If you have something that's a date or a date/time then it's always better to use the right datatype for that. You get many benefits in terms of being able to do functions on it, having comparisons work, etc. It will also map well to the date/time types in your preferred language.
I think storing the dates as a datetime field makes the most sense, but I agree, it's not the easiest to deal with. I have a function in my date library that turns MySQL datetime values into a PHP timestamp.
function datetime_to_timestamp($datetime)
{
$timestamp = strtotime( $datetime );
return $timestamp;
}
Hope that helps.
PHP function strtotime($datetime) converts mysql datetime fields to unix timestamp and works really well. Why using an extra wrapper?

mysql date xammp

i've set a field as date in xammp server. but when i'm inserting a values, its saving like 0000-00-00.. i've even input it manually like 2010-10-10 but still saveing 000... is the problem with my code or the xammp server??? or is there any way to configure the date format in xammp???
$today = date('Y-m-d');
"/>
update.php
$date = $_GET['datee'];
$qry = "INSERT INTO course_detail(userid, course_id, hours_complete, week_no, date) VALUES('$member_id','$fname','$hour','$week', '$date')";
$result = #mysql_query($qry);
It's probably with your code. Have you quoted the date string when inserting?
INSERT INTO mytable SET datefield='2010-10-10'
This is definitely not a XAMPP issue. I highly recommend you take a look at this link MySQL Documentation on Datetime
MySQL Date has a fixed syntax and it generally falls along the lines of YYYY-MM-DD or YYYYMMDD. I'm sure you're using a presentation layer over MySQL such as *.NET (ASP,winforms), you can reformat the date generated by MySQL to match your locale. Here's how you do it on the Microsoft stack MSDN Globalization Step-by-Step, similar methods are available for whatever else technology you may be using.

Categories