Displaying strtotime() effectively in MySQL/PHP query

落爺英雄遲暮 提交于 2019-12-31 03:51:05

问题


This is my PHP/MySQL query, as mentioned at Displaying links in PHP/MySQL?:

http://pastebin.com/5Td9Bybn

I'll admit, I've forgotten how to use the strtotime() function effectively, as I want to show my show times in this format:

06:00

10:00

(without the :00 at the end, e.g. 10:00:00)

The code displays this error now:

Parse error: syntax error, unexpected T_ECHO in C:\www\vhosts\localradio\schedsun.php on line 11

I've fixed the errors in the previous question, how could I adapt this if I wanted to show the time as:

6:00am

06:00

6am

(just for testing sake, within the parameters of the code on Pastebin).

I'm basically asking simply as I'm doing my own refresher course in other parts of PHP, having spent so long learning arrays etc.

Finally, do I need as a variable or not - or is $result_ar not needed?

Thank you for your help on the last question!


回答1:


You can do it directly in MySQL, which saves you the overhead of strtotime()'s parseing overhead, and guaranteed to produce the right results, as strtotime can guess wrong occasionally.

SELECT DATE_FORMAT(yourdatefield, '%h:%i') ...



回答2:


The PHP function strtotime() has the following usage:

int strtotime ( string $time [, int $now ] )

This means that you pass in a string value for the time, and optionally a value for the current time, which is a UNIX timestamp. The value that is returned is an integer which is a UNIX timestamp.

An example of this usage is as follows, where the date passed to strtotime() might be a date from a database query or similar:

$ts = strtotime('2007-12-21');

This will return into the $ts variable the value 1198148400, which is the UNIX timestamp for the date December 21st 2007. This can be confirmed using the date() function like so:

echo date('Y-m-d', 1198148400);
// echos 2007-12-21

strtotime() is able to parse a wide variety of strings and convert them to the appropriate timestamp, using actual dates and also strings such as "next week", "next tuesday", "last thursday", "2 weeks ago" and so on. Here are some examples:

$ts = strtotime('21 december 2007');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';

This will display the following:

1198148400 2007-12-21

If today is December 21st, then the following:

$ts = strtotime('next week');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';

$ts = strtotime('next tuesday');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';

$ts = strtotime('last thursday');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';

$ts = strtotime('2 weeks ago');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';

$ts = strtotime('+ 1 month');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';

will display the following:

1199006542
2007-12-30
1198494000
2007-12-25
1198062000
2007-12-20
1197192142
2007-12-09
1201080142
2008-01-23



回答3:


Option 1

strtotime() will give you a timestamp, you use the function date() to format the display of time and/or date.

06:00

<?php echo date('H:i', strtotime($result_ar['airtime'])); ?>

6:00am

<?php echo date('g:ia', strtotime($result_ar['airtime'])); ?>

6am

<?php echo date('ga', strtotime($result_ar['airtime'])); ?>

Read about date() and its formatting on php.net.


Option 2

Do it in MySQL, as @Marc B suggests.

Change this line

$result = mysql_query("SELECT * FROM presenters1;", $connection) or die("error querying database");

to

$result = mysql_query("SELECT *, TIME_FORMAT(airtime, '%h:%i') `airtime` FROM presenters1;", $connection) or die("error querying database");

Then, change this line:

<div class="time"><? php echo strotime ('H') $result_ar['airtime']; ?></div>

to

<div class="time"><?php echo $result_ar['airtime']; ?></div>



回答4:


You have a space between the <? and the word php. I think that is causing the unexpected echo error. You should have <?php



来源:https://stackoverflow.com/questions/6713552/displaying-strtotime-effectively-in-mysql-php-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!