have a database with data and time
example: 2013-06-04 08:20:00
need to convert that to
example: 1378478351000
so i can add that number to jquery script event calendar
when i use this php code
$exc_date = $row_Recordset1['exc_date'];
$exc_date = microtime(true) *1000 ;
echo $exc_date;
it works right but it shows me the current date and time not the date and time saved at database,
can somone please help , thanks
If you want to avoid the calculations in PHP, add a computed column using unix_timestamp and str_to_date to your query:
select (
unix_timestamp(str_to_date(TimeStrColumnName, '%Y-%m-%d %H:%i:%s')) * 1000)
as utime,
# ... the rest of your query as before
if the MySQL column is already a date/time field (instead of a string), this will do it:
select (unix_timestamp(TimeStampColumnName) * 1000) as utime,
# ... the rest of your query as before
Of course it does, as you overwrite the database values in the second line by putting microtime instead in it.
You will not be able to get the microtime, when you just have the format of date in the database - so you should use 0. microtime is returning the value of the actual microtime at the moment.
You should do something like that to get time + microtime:
$exec_date = strtotime($row_Recordset1[exec_date"]) . "000";
More information here. The problem with this is, that you will get it as a string, so you will need to convert it to an integer:
$exec_date = (int) $exec_date;
Hope it helps you.
Related
I have a special requirement where need to sum time columns, but for few record sum time is coming '08:60:02' so i need to get it as '09:00:02'. I tried to convert it to seconds first and then convert back to h:m:s but it's giving null as result.
$a = date_create_from_format("H:i:s", "08:60:02");
echo $a->format("H:i:s");
shows
09:00:02
you can try mysql functions like
SELECT round((SUM((08:60:02))/60)/100, 1);
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.
Hey guys so, im doing this form with a input that will contain the time and i want to make it so that when users choose the time (for example 8:30 AM), it will be stored in mysql, and will be output by php with the exact time (8:30 AM).
Here is the form. A simple select button with increments of 30 on the time.
http://jsfiddle.net/jXVPS/
The main problem is i dont know how mysql will recognize if it is AM/PM?
What values i should put for the select fields, how i would insert it into the mysql database, and have mysql or php recognize wheather it is 1:00 AM or 1:00 PM. Is there some sort of 24 hour clock in mysql that makes it recognize wheather or not it is AM/PM?
Use TIME column type:
TIME
A time. The range is '-838:59:59' to '838:59:59'. MySQL displays TIME values in 'HH:MM:SS' format, but allows you to assign values to TIME columns using either strings or numbers.
It will parse the values in your select options nicely.
Edit: Example of use:
// saving in the db
$sql = "
INSERT INTO table_name SET
# ...
name_of_the_time_column = '" . mysql_real_escape_string($_POST['start_time']) . "'";
// retrieving from the db
$sql = "
SELECT id, ..., TIME_FORMAT(name_of_the_time_column, '%h:%i:%s %p') AS formatted_time
FROM table_name
# ...
";
Here's MySQL's time/date stuff. I think the TIME type would be best for you since you don't need a date, and the format you've got for your values should fit well. I think you can just put them as strings exactly like that.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-types.html
After you submit thru form submission or ajax and your responding PHP page takes over, perform a query with the following string in the PHP:
"INSERT INTO table (userId, time, ...) VALUES (..., '$inTime', ...)"
And then echo it out since you want the next page to repeat it.
Response to comment
I figure you mean that wou want 13:00:00 to be 1:00 PM when you echo it out later right?
Theres probably something you can do with time functions for either PHP or MySQL (PHP below, MySQL above), but I don't know from memory and this is simple enough you can just do it on your own.
http://www.php.net/manual/en/ref.datetime.php
$time = //get time field from MySQL
list($h, $m, $s) = explode($time, ':');
if ($h > 12) {$h -= 12; $amOrPm = 'PM'}; else {$amOrPm = 'AM';}
if ($h == 0) {$h = 12;}
echo "$h:$m $amOrPm";
PS - 13:00:00 is 1pm, not am.
Not tested :
INSERT INTO myTable
SET time = TIME(STR_TO_DATE('8:30:00 AM','%h:%m:%s %p'))
all - fairly simple query question that's been hounding me: How do I query for entries with a date and time only greater than now, or when the query is run (page requested)?
I've seen some examples but they aren't good enough for me to modify. Here's my code:
$todaysDate = date("Y-m-d h:i:s");
$params = array('select'=>'*', 'limit'=>3, 'orderby'=>'t.event_StartDate ASC', 't.event_StartDate < "$todaysDate"' );
An example of the variable "t.event_StartDate" outputs "2010-12-10 22:18:42" so I assume that may be how it's saved in the database.
I suspect I'm not building the date correctly or I need to be using a time function I'm not familiar with in MySQL. Help? This is for outputting a series of events with date and time.
I think you reverse the use <, it should be >
try
SELECT ... WHERE t.event_StartDate>NOW();
/* you don't even need to set $todaysDate */
t.event_StartDate > NOW() don't will returns nothing? a date higher of now lol. I'm confused.
I have a collection of time records in a database in the format '09:51:06' (hour, minute, second).
I have a query which retrieves a bunch of these times from the database, but I only care for the Hour reference.
How can I get rid of the rest of the string and ad just the hour into an array? So in the example above, I just want to keep '09'.
I've looked into exploding and substrings, but can't seem to figure it out.
Thanks.
Exploding the string would look like this (you probably want to add intval() to be able to use it as a real number):
$hours = array_shift(explode(':', '09:51:06'));
But you are probably looking for the right query instead of doing this afterwards. If you are dealing with a time-type, you can use MySQL's date and time functions (HOUR() in this case):
SELECT HOUR(`time_column`) AS `hour` FROM `your_table`
Or from the database itself
SELECT TIME_FORMAT(field_name, '%H') as return_hour from table_name
$time_string='09:51:06';
$your_array[$array_index]=substr($time_string, 0, 2);
for more info on substr
I guess you can do this either thru string manipulation or time/date. Any worthwhile date way would be on the SQL side, PHP has mktime and date functions but you would have get the hour you're looking for intermediately along the way if you were to construct a date anyway
String
$thisHour = array_shift(explode(':', $timeString)); //this gets hour
$thisHour - substr($timeString, 0, 2); // or this
$hours[] = $thisHour; //this adds to array