So I have a timestamp from mysql database in a format: 2016-08-15 15:35:53
I receive it like this: $row['date'] and I want to have just 15:35 for example, i.e. HH:MM format. Would be even better if it were a 12-hour format.
I assume it is passes the whole timestamp as a string?
Thank you!
Why not do it in MySQL directly? strtotime is a handy function, but it's also a waste of CPU resources, as you'll be forcing mysql and PHP to take the mysql internal datetime value, format it to a string, which you then convert to a php unix timestamp, and then convert BACK to string.
Just do the conversions ONCE:
SELECT time(yourfield) FROM ...
SELECT DATE_FORMAT('%H:%i', yourfield) FROM ...
Relevant docs: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
Solved with:
$time = date('H:i', strtotime($row['date']));
I don't know if I should delete the question... Thanks splash58
Related
please i need your help i created a comment box in my page, stored inside phpmyadmin with time type DATETIME. the problem am having is the time always display in 24 hour format and i want it to display in 12 hour format (PM/AM) and be stored inside mysql. i have tried using date() function at the same time i used date("y-m-d H:i:s") instead of now())function but the result i keep on getting is in 24 hour format
see the code
$insert = mysql_query("INSERT INTO test (name,comment,whenadded) VALUE ('$name','$comment', now())");
With this code i get the result in 24 hour time format.
whenadded is the DATETIME variable name.
thank you in advance.
You want to store the date as DATETIME in the MySQL DB, thats good practice.
For output, use PHP's date() function. Look at this answer. Or you use MySQLs date_format() function.
SELECT date_format(whenadded, 'Y-m-d h:i') AS my_date FROM ...
The php documentation should help http://www.php.net/manual/en/function.date.php
what you are looking of is date(y-m-d g:i a) wich will give something like "2013-12-30 4:38 pm"
Let the mysql decide its date format, it's mostly irrelevant for you.
What you need, is to properly format your output data, like:
echo date("y-m-d h:i:s A", strtotime($date));
Where $date is the variable you get from MySQL.
In no particular order:
phpMyAdmin is not a database engine. MySQL is.
Dates are not stored in any particular format. You give format when you convert them to strings.
The mysql_... legacy extension is deprecated, insecure, triggers a notice in latest PHP versions and will be removed.
Your code is probably vulnerable to SQL Injection.
The H format code means: 24-hour format of an hour with leading zeros.
It's good to save the data within 24Hours Format, but you can show it within 12Hours plus am/pm
date("d/m/Y - g:i A");
I have a MySQL field which stores times using the 'time' field type, such as '12:30:00' i.e. in the HH:MM:SS format.
In my PHP app I want to convert such HH:MM:SS data into a more readable string, such as '12.30pm'.
Is there any function for doing this, or will I have to do it by other means?
Many thanks!
I don't have a machine to test it on at the moment, but I would look at: strtotime and date. I believe that the following should be what you want:
$print_time = date("h.ia", strtotime($sql_time));
Where $print_time is the pretty printable time string and $sql_time is the SQL timestamp string.
If you don't mind changing you MySQL code you can just use MySQL's DATE_FORMAT() on the fetc:
SELECT DATE_FORMAT(table_name.time_col, '%l:%i %p') AS myFriendlyDate;
Why not use the datetime functions built into MySQL?
DATE_FORMAT will allow you to manipulated it however you want.
Or any of the other functions may help you with it ...
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
In my php application I have this code:
<?php echo date("d/m/ Y ",strtotime($row["m_date"]));?>
In it, $row["m_date"] is fetching from a database.
The problem is that all the dates are printing perfectly except 27/2/2011. It's printing 1/1/1970 instead.
The date in the database is fine, and prints correctly in a PDF.
I'll assume you're getting the date from the database as the string 27/2/2011 because that's most probably what happens (correct me if I'm wrong).
PHP considers the string 27/2/2011 as being in the m/d/Y format, not d/m/Y and tries to parse under that assumption. Because the date is not valid under that format strtotime returns false. Giving false as the timestamp parameter to date is taken as 0, which is the timestamp for January 1st 1970.
What you need to do is either get your date in another format (or better still, as a timestamp) from the database, or parse it yourself (say using explode).
Good luck,
Alin
The database should be able to return the date to you as a UNIX timestamp. For example, MySQL has the UNIX_TIMESTAMP() function.
SELECT UNIX_TIMESTAMP(date_column) FROM table;
Postgres has date_part
SELECT DATE_PART('epoch', date_column) FROM table;
Most other databases should have similar features. If you can get the date out as a UNIX time stamp you can pass that directly to date() without having to use strtotime() as well.
All of this does of course assume you're using a temporal datatype for the columns in question (timestamp, datetime, timestamp with time zone, etc) and not just storing a string. You are using a temporal type, right? If not, then why not?
if you are storing the date in the database as a timestamp this should work
<?php echo date("d/m/Y",$row["m_date"]);?>
if you are storing the date in the database as a date or datetime this should work
<?php echo date("d/m/Y",strtotime($row["m_date"]));?>
How is the m_date stored in the databases? Is it a datetime object? Or a string.
Problem with strtotime is that it isn't real good at deciphering written dates. So something like 27/2/2011 gives problems while 27/02/2011 gives no problems at all.
So there are 2 solutions:
Make sure all the dates that get entered into the database are of the correct format (dd/mm/yyyy).
Write a regular expression that adds a leading zero to all single characters.
I think this is a simple question. We have a MySQL database with a DATE field, the date is stored in US format (2010-06-01).
In my PHP page where I'll display the date, I simply want to convert this date into a UK format (01-06-2010).
Any help and advice appreciated!
Thanks,
Homer.
You didn't specify and your example is ambiguous, but I'll assume you're talking about outputting in day-month-year format. You can use strtotime to parse a string date into a unix timestamp, and then give that timestamp to date along with the format that you'd like to output your date in:
date("d-m-Y", strtotime($date_from_mysql));
The manual page for date lists all the formatting options that you can give. In this case, d represents the day as a zero-padded number, m represents the month as a zero-padded number, and Y represents the year as a four digit number. Any characters that don't have a specific meaning for date come across as-is.
You can do it right from mysql using DATE_FORMAT() function.
example : "SELECT DATE_FORMAT(date_column,'%d-%m-%Y') as my_formated_date;" will do what you need , just make sure to use in fetch method my_formated_date column
You can parse the date with this function:
http://php.net/manual/en/function.strtotime.php
It will return an integer which is number of seconds since 1970 (called a Unix timestamp).
Then use this function to format that number any way you like:
http://ca3.php.net/manual/en/function.date.php
You can also create a Date object in PHP using the DateTime::createFromFormat feature.
<?php
$date = DateTime::createFromFormat('Y-m-d', $sql_date);
echo $date->format('d-m-Y');
?>
I have a mysql table field set as time type which stores data in the HH:mm:ss format. So when I list the data, it prints as, for example, 16:30:00. But I want to display hh:mm part only (not the seconds).
In case of datetime types, I can do date('H:i', '2010-03-16 16:30:00'). I mean I can retrieve any part. I wonder if there is any similar way like this for time only fields??
Please see, I can manipulate the time string to get rid of seconds in time part using str_replace, explode etc, I just wonder if there is any standard function there which I am not aware of.
If you want to do this with PHP, you'd have to get a timestamp from the time first, e.g.
echo date('H:i', strtotime('16:30:00'));
will output 16:30. Strtotime will assume the time is for the current date then.
You can let MySQL return the data in the format you want using Date_Format()
edit: as fireeyedboy pointed out there's also a TIME_FORMAT() function.
Another approach is:
SELECT UNIX_TIMESTAMP(time_field) AS tstamp FROM table;
Then you can use PHP's date() function to format it.
$time = date('H:i', $tstamp);