Convert SQL Time using PHP? - php

I want to convert MySQL Time data type using PHP and Javascript. I know it can be done using FORMAT_TIME of MySql, but I would like to do the same with php. The Time format is hh:mm:ss by default and I would like to convert it to hh:mm.

Just feed the date into the following function(s):
<?php echo date("h:i", strtotime($date)); ?>

//any date is ok, we care only about the hours and minutes
// $your_date is what comes from db in format hh:mm:ss
$date = new DateTime('2000-01-01 '.$your_date);
echo $date->format('H:i');

I think you could create a DateTime object and then use format("h:i") to get the desired output

Related

How do get this date/time format?

I saw a date/time format in a PHP code that has something like this:
2015-06-19T00:38:04Z
How can I generate a datetime having that kind of format?
I know this kind of format date("Y-m-d H:i:s"); outputs different 2015-06-19T00:38:04Z because it has a T and a Z.
So it's like I want to get the current date/time having a T and a Z
Escape T and Z in format
echo date("Y-m-d\TH:i:s\Z"); // 2015-06-17T16:50:50Z
UPDATE Thanks to Lithis. To take a GMT/UTC time
echo gmdate("Y-m-d\TH:i:s\Z"); // 2015-06-17T17:06:06Z

Get date from database in php

i am storing date and time in database using php using gmdate() function in format "Y-m-d H:i:s". for e.g.
2014-03-10 12:35:55
Now,on getting this data into a php variable,for e.g.
$temp=2014-03-10 12:35:55
can i extract and display only the DATE portion excluding the TIME portion??
I am new to date and time manipulation.Any help?
you can get directly the date from database like this
select DATE(column_datetime) as date from yourtable
As Pramod answered,
date('Y-m-d', strtotime($temp));
works.
date('Y-m-d', strtotime($temp));
The above statement returns the date from datetime format
The following code example is adapted by this thread: How to convert date to timestamp in PHP?
There you can also read why it is not safe to rely on strtotime (answer by daremon)
$temp = '2014-03-10 12:35:55';
$a = strptime($temp, '%Y-%m-%d');
$timestamp = mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900);
Edit
From the PHP manual
Note: This function is not implemented on Windows platforms.
Try this:
Echo date('Y-M-D', $var);

How to insert time with am/pm format into the database

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.

UTC datetime conversion

The values that I received from my device are: 090211 = ddmmyy and 062123 = hhmmss in UTC.
But I found that the time is always 8 hours later if compared to the time that I need. It is because the time for Malaysia is +8:00. First I would like to add 8 hour, and finally I would like to store this kind of date format into my MySQL database as "2011-02-09 06:21:23". How would I convert these values?
To convert in PHP to a datetime, you will need the function DateTime::createFromFormat(); This function will return a DateTime.
Using this function you can also pass the timezone as a parameter.
Example:
$date = DateTime::createFromFormat( 'dmy Gms', '090211 062123', new DateTimeZone("Europe/Amsterdam") );
You can create a output a following:
echo $date->format('Y-m-d H:i:s');
or UNIX timestamp for the MySQL:
echo $date->format('U');
Hope this helps!
PHP has both localtime and gettimeofday functions, are you by chance using the wrong one (or misinterpreting its results)?

Inserting DATE TIMESTAMP Value to MySQL Using PHP

Using php I am inserting or updating the mysql database with create date or modified date using the variables
$datestring = "%Y:%m:%d %h:%i:%s";
$time = time();
$createdate= mdate($datestring, $time);
In this $createdate will be the variable I use to insert or update the table. But it's updating the wrong value. It's not the server time or localtime. Mostly it's 30 mins delay with the server's time.
Use date() function of PHP
$createdate= date('Y-m-d H:i:s');
Edit: after some googling it looks like you're using CodeIgniter. You should have mentioned that in your question.
The format string you're using doesn't match MySQL's date format. You want to use:
$datestring = '%Y-%m-%d %H:%i:%s';
use in mysql query like DATE_FORMAT(purchaseDate, "%Y:%m:%d %h:%i:%s") function

Categories