Hello I'd doing a project which is related to android , php and mysql, there is this function that i need to get the user input from android device, and post it to php script which then query the database, one of the inputs is a time string the format is "XX:XX:XX" , this string will be stored as a time in mysql, my question is how do I convert the string into mysql time format in php ? (only time, NO DATE)
according to http://dev.mysql.com/doc/refman/5.0/en/time.html, the TIME type should accept strings formatted like 'HH:MM:SS' so you should be able to directly save your data without the need to convert anything.
Unix time or full date format ?
for unix time do :
<?php
$time=strtotime($input);
?>
and full-date format :
<?php
$time=date('c',strtotime($input));
?>
any else ?, good luck
To convert the date in a mysql Timestamp format you can use the following snippet :
$mysqlTime = date("H:i:s", strtotime("20:30:10"));
the variable $mysqlTime will hold it for you.
I think I clearly understood your question.
Related
In an SQL database I'm storing various dates, such as date of birth and date they joined my system, in the standard SQL format YYYY-mm-dd, however I wish to display these to my British users (all my users) in the format dd-mm-YYYY.
I've tried pretty much everything I found online about doing this, however cannot decipher how it's done correctly. The code I list below is what I am currently using, however it does not display the correct date stored in the database and instead uses a completely random date of 01-01-1970. Some assistance on resolving this issue would be greatly appreciated.
while($row = $results->fetch_assoc()){
array_push($_SESSION["ActQueue"], array($row["username"], $row["surname"], $row["forename"], date('d-m-Y', $row["dob"]), $row["gender"], $row["joined"]));
}
$data = 0;
echo json_encode(['Username'=>$_SESSION["ActQueue"][0][0], 'Surname'=>$_SESSION["ActQueue"][0][1],'Forename'=>$_SESSION["ActQueue"][0][2],'DoB'=>$_SESSION["ActQueue"][0][3], 'Gender'=>$_SESSION["ActQueue"][0][4], 'Joined'=>$_SESSION["ActQueue"][0][5]]);
You need to convert your plain text date to time before passing to date() function
date('d-m-Y', strtotime($row["dob"]))
The date you receive 01-01-1970 its not a random date but its actually the first date from unix system
You need to use :
date('d-m-Y', strtotime($row["dob"]))
strtotime
You can use this this code to format your date
(new \DateTime($row["dob"]))->format('d-m-Y');
I have a calendar in html form and I want to insert this date into MySQL. The default MySQL date is 0000-00-00. But in my country the format is DD/MM/YYYY. So what to do to fix it. Thank you. I am using PHP.
You must use one format in your HTML page, and another format in your database.
So, if you want to store a date like this '12/05/2008' into mySql, you must transform it like this:
$date = '12/05/2008';
$dateToStore = date('Y-m-d', strtotime(str_replace('/','-',$date)));
And if you wonder why, you need to replace the '/' with '-' to make php know that the first part of the data string is the day, and then the month (as I think is your case).
MySQL the date format is always YYYY-MM-DD. To convert it to another format, you need to manually convert the retrieved date to the desired format like
$displayDate=date("d/M/Y", strtotime($mysqldate));
Method 1
You cant insert into DD/MM/YYYY format. Instead while rendering it in view file you can change into desired format.
<?php
$date = $result['db_date']; // I ASSUMED YOUR DB FIELD IS db_date
$desiredFormat = date('d/m/Y', strtotime($date)); // CONVERTING INTO YOUR FORMAT
echo '<pre>'; print_r($desiredFormat); // DISPLAYING IT
?>
Method 2
You can retrieve from database in your desired format using below
SELECT *, DATE_FORMAT(YOUR_DATE_FIELD, "%m/%d/%Y") AS date FROM YOUR_TABLE;
Use MySQL STR_TO_DATE
Try this mysql query :-
INSERT INTO `table`(`date`) VALUES (STR_TO_DATE('10/10/2015', '%d/%m/%Y'))
Hi i'm using php5 and mysql.
I have a time like that 10:00, 10:45 ... and i shoult put it a mysql database in form 'hh:mm:ss'
I tryed in different way but nothing works.
What i try was:
$time= time('H:i:s', $mytime)
$time= time('H:i:s', strtotime($mytime))
$time= strtotime($mytime)
$time= strtotime($mytime.':00')
$sql = "INSERT INTO table SET `time`='$mytime';
Hint: be sure you are using proper Mysql field format
Another hint: do not use randomly picked PHP functions. At least try to read the function description in the manual. It can give you idea if this function suit your needs or not.
strtotime need a complete date as input and outputs an int. You don't need an int for mysql, you only need the string you have plus the seconds info:
$time = $mytime.':00';
Then insert time in db, as #Your_Common_Sense says, you need to user TIME datatype in order to insert a time in this format.
I am writing a query in php using a string sent from a android java application.
The query is something like :
$insertSQL = sprintf("INSERT INTO app_DuckTag (taste) VALUES (%s) WHERE species=%s AND timestamp=%s",
GetSQLValueString($_POST['taste'], "text"),
GetSQLValueString($_POST['species'], "text"),
GetSQLValueString($_POST['timestamp'], "text"));
But I doubt timestamp is stored as a string inside MySQL.
How should I convert the string to time format as in MySQL?
The strtotime(string) php function converts it to unix time.
But the MySQL stores it differently, I guess. Thank you.
EDIT: This is how it shows up in MySQL phpmyadmin: 2011-08-16 17:10:45
EDIT: My query is wrong though. Cannon use a where clause with Insert into.
The query has to be UPDATE .... SET ... = ... WHERE ....
But the accepted answer is the correct way to use the time inside the WHERE clause.
This should be all:
date("Y-m-d H:i:s", strtotime($_POST['timestamp']));
if you want to check for timezones and such you should use strftime instead of date
If you can get the input 'string' (which you haven't provided the format that you're receiving it in) to a Unix timestamp value, you can easily convert it to a MySQL datetime format like this:
$mysqldatetime = date("Y-m-d H:i:s", $unixTimeStampValue);
This will produce a string similar to the following:
2011-08-18 16:31:32
Which is the format of the MySQL datetime format. If you are using a different time format, then the first argument to the date function will be different.
See the manual for the date function for more information and other ways you can format the value that it returns.
Edit
You are receiving a string formatted in MySQL datetime format. After sanitizing it, you can insert it directly into the database. To MySQL it may be a 'datetime' data type, but to PHP it is simply a string (just like your entire SQL query is nothing more than a string to PHP).
$timestamp = mysql_real_escape_string($_POST['timestamp']);
You should be able to safely insert that into your database for the timestamp field.
There is a function FROM_UNIXTIME in mysql. Why don't you want to use it?
I am trying to display a time I have in my database. I managed to have it display a time in the correct format for what I need, but for some reason, it is only displaying '4:00' every time.
Here is my code:
date('g:i', strtotime($row['startTime']))
An example of I have the time displayed in my database is like this: 00:12:30
Why is it showing '4:00' every time?
strtotime expects a datetime format ... you should do
date('g:i', strtotime('01 January 2009 ' . $row['startTime']))
Whats the underlying database, and what datatype does the startTime column have? Peering at the closest php code I have, strtoime works fine with a DATETIME representation in the DB (MySQL).
strtotime converts a date time string to a Unix timestamp.
Perhaps your $row['startTime'] doesn't qualify as a date time string.
None of the examples here discussed a date time string which did not include a date.
The link also said that if strtotime is confused, it returns random results. I would add a few more format characters and see what else is returned.
As noted the problem is the use of strtotime(). The following works on my machine, if it's of any use:
$date_text = $row['startTime']; // assuming the format "00:12:30"
list($hrs,$mins,$secs) = explode(":",$date_text); // in response to the question in the comments
/* the explode() takes the string "00:12:30" and breaks into three components "00","12" and "30".
these components are named, by their order in the array formed by explode(), as $hrs, $mins and $secs.
see: http://us3.php.net/manual/en/function.explode.php
and: http://us3.php.net/manual/en/function.list.php
*/
echo "<p>" . date("g:i",mktime($hrs,$mins,$secs)) . "</p>";