Displaying strtotime() effectively in MySQL/PHP query

前端 未结 4 628
我寻月下人不归
我寻月下人不归 2021-01-23 11:09

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()

相关标签:
4条回答
  • 2021-01-23 11:31

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

    0 讨论(0)
  • 2021-01-23 11:41

    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
    
    0 讨论(0)
  • 2021-01-23 11:48

    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') ...
    
    0 讨论(0)
  • 2021-01-23 11:49

    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>
    
    0 讨论(0)
提交回复
热议问题