How can I use PHP to display a MYSQL datetime value without displaying the seconds?
-
How can I strip the seconds off when displaying a MYSQL datetime variable, using only PHP commands, not MYSQL commands like "DATE_FORMAT"? I have a form where users enter a few dates and times. I don't care about seconds, just the year, month, day, hours, minutes. I'm using PHP with MYSQL, and the MYSQL values are in datetime format. When I display the contents of those variables, it shows them as, for example: 2006/11/28 17:26:00 What I would like displayed is: 2006/11/28 17:26 I know this can be done with the DATE_FORMAT command in a MYSQL query, but the problem is that I have quite a few queries, all in the form of "SELECT * FROM table WHERE blahblahblah" (which gathers lots of other information besides datetimes), and if I make separate queries in the form "SELECT DATE_FORMAT (startTime, blahblahblah) AS startTime" for all the time values I call up, I'm going to be making my code much longer and harder for other people (who, like myself, are non-programmers with limited PHP knowledge) to work on. Is there any way that's relatively clean/simple on the PHP end to take, for example, a variable called $startTime, with a value like "2006-11-11 12:13:00", and display it on a page as "2006-11-11 12:13"? Yes, I have Googled like a motherfucker, but to no avail.
-
Answer:
And converting to a unix timestamp can be done with strtotime, like so: date("Y/m/d H:m", strtotime($your_mysql_datevar));
Bugbread at Ask.Metafilter.Com Visit the source
Other answers
$startTime = substr($startTime, 0, strlen($startTime)-3);
Blazecock Pileon
To round down to the nearest minute, convert to a unix timestamp and divide by 60. Also, investigate the date() function, eg date ("H:m:00", $unix_timestamp);
Leon
BTW, "SELECT *" is considered bad practice. You can also do: SELECT *, DATE_FORMAT(blah) AS parsedDate
Leon
You can use http://uk.php.net/manual/en/function.strtotime.php to convert to unix timestamp, then http://uk.php.net/manual/en/function.date.php to format it. this should work: date("Y:M:d H:m", strtotime($startTime))
MetaMonkey
Oops, I'm late and made a small mistake, more like: date("Y:m:d H:i", strtotime($startTime))
MetaMonkey
If you are doing this often, I would suggest you use a substring solution to just cut off the last three characters of the string rather than using strtotime(). While it is a great and useful function, it is extremely slow, so if this is something that will be used a lot, you will have much better results with a simple string manipulation.
chrisroberts
Wow, everyone made this way too complicated. Blazecock Pileon's way is the way I'd do it.
jesirose
Good answers, all. It's working just as I wanted, now. Cheers!
Bugbread
You could also create a view as such:CREATE VIEW blahblahblah_nosec AS SELECT col1, col2, DATE_FORMAT('myformat') AS col3, col4, ... FROM blahblahblahThen just select from this view whenever you want the date formatted.
sbutler
Related Q & A:
- How can I use a button to retrieve a phone 'number' from contacts?Best solution by Stack Overflow
- How Can I Use A Picture For My Avatar?Best solution by Yahoo! Answers
- How can I use my PC with a wireless printer?Best solution by pcmag.com
- How can i use my laptop as a monitor for my xbox 360?Best solution by Yahoo! Answers
- How can I use a prepaid credit card?Best solution by ehow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.