【问题标题】:Date format (php&mysql)日期格式(php mysql)
【发布时间】:2023-03-24 23:19:01
【问题描述】:

我想要德语日期格式。在我的 phpmyadmin 表中,我的格式类型为“DATE”,现在我在查询中苦苦挣扎。

$sql    =   "SELECT * 
         FROM workeroffers
         WHERE 0=0"
         .( $w_destrict ? " AND w_destrict LIKE '$w_destrict'":"" )
         .( $w_select ? " AND w_select LIKE '$w_select'":"" )
         .( $w_mjobs > "" ? " AND ( ".ltrim( $w_mjobs," OR" )." )":"" )
         .( $w_mdestrict > "" ? " AND ( ".ltrim( $w_mdestrict," OR" )." )":"" )
         ." ORDER BY DATE_FORMAT( w_date, '%d.%m.%Y') AS w_date ASC;"; 

这个查询出了点问题,我没有得到任何结果。有什么想法吗? 对不起,我第一次这样做。

在这里它可以工作,但日期格式错误:

$sql    =   "SELECT * 
         FROM joboffers
         WHERE 0=0"
         .( $j_destrict ? " AND j_destrict LIKE '".$j_destrict."'":"" )
         .( $j_select ? " AND j_select LIKE '".$j_select."'":"" )
         .( $j_mjobs > "" ? " AND ( ".ltrim( $j_mjobs," OR" )." )":"" )
         .( $j_mdestrict > "" ? " AND ( ".ltrim( $j_mdestrict," OR" )." )":"" )
         ." ORDER BY j_date ASC;";

这是一张不同的桌子。上面只是一个尝试。我坚信,它可能会朝着正确的方向发展。

这是我的结果代码:

<div class="table_padding">
<table class="responsive">
    <tr>
        <th>Jobart</th>
        <th>Stadteil</th>
        <th>Berufsfeld</th>
        <th class="table_titel">Title</th>
        <th>Vergütung</th>
        <th>Gesucht ab(Datum)</th>
        <th>Weitere Informationen</th>
    </tr>
    <?php

        foreach ( $inseration as $value ) { 

    ?>

    <tr>
        <td><?= $value['w_select'] ?></td>
        <td><?= $value['w_destrict'] ?></td>
        <td><?= $value['w_cat'] ?></td>
        <td class="table_titel"><?= $value['w_title'] ?></td>
        <td><?= $value['w_cash'] ?></td>
        <td><?= $value['w_date'] ?></td>
        <td><a href="index.php?site=Jobsuche_Inserat&w_id=

            <?php
                echo $value['w_id'];

                    if( $w_select ){
                        echo '&w_select=' . $value['w_select'];
                    }
                    if( $w_destrict ){ echo '&w_destrict=' . $value['w_destrict'];
                    }
                    if( $w_mjobs > "" ){ 
                    echo '&matches_job=' . implode(',', $_REQUEST['matches_job']); 
                    }
                    if( $w_mdestrict > "" ){ 
                    echo '&matches_destrict=' . implode( ',', $_REQUEST['matches_destrict'] ); 
                    }

            ?>

        ">Weiter</a></td>
    </tr>
<?php

        }

?>
</table>

可能我错了,我只想输出德语日期格式!!

编辑: 好的,很酷,它正在使用正确的日期,但现在没有按日期排序。我做错了什么?

$sql    =   "SELECT DATE_FORMAT(w_date, '%d.%m.%Y') AS w_date, w_id, w_userid, w_select, w_cat, w_destrict, w_cash, w_title, w_desc, w_pic, w_email, w_phone, w_fax
         FROM workeroffers
         WHERE 0=0"
         .( $w_destrict ? " AND w_destrict LIKE '$w_destrict'":"" )
         .( $w_select ? " AND w_select LIKE '$w_select'":"" )
         .( $w_mjobs > "" ? " AND ( ".ltrim( $w_mjobs," OR" )." )":"" )
         .( $w_mdestrict > "" ? " AND ( ".ltrim( $w_mdestrict," OR" )." )":"" )
         ." ORDER BY w_date ASC;";

【问题讨论】:

  • $w_mjobs &gt; "" 呵呵。 ORDER BY DATE_FORMAT( w_date, '%d.%m.%Y') AS w_date ASC呵呵..
  • 您希望在结果中返回日期格式,还是按德语日期格式对结果排序?
  • both.. 我想在结果中包含日期并按日期排序,但采用德语日期格式
  • 你能在表上显示你的表定义和示例数据吗?我不清楚您为什么要尝试根据日期格式进行订购。如果您有一个日期字段,您应该根据它进行排序,您可以在查询的选择部分或应用程序中更改格式。
  • SELECT DATE_FORMAT( w_date, '%d.%m.%Y') AS w_date, [other fields] FROM ... 无论如何,你应该改掉使用SELECT * 的坏习惯,因为如果数据库架构发生变化,它会使你的代码更难阅读并且可能很脆弱。

标签: php mysql sql


【解决方案1】:

尝试用您的日期格式中的破折号替换您的句点,如下所示:

." ORDER BY DATE_FORMAT( w_date, '%d-%m-%Y') AS w_date ASC;";  

【讨论】:

  • 它不工作。我不确定,如果我使用“DATE_FORMAT”函数是否正确。
猜你喜欢
  • 2012-08-20
  • 1970-01-01
  • 1970-01-01
  • 2013-11-21
  • 2013-10-05
  • 1970-01-01
  • 2017-12-17
  • 2011-10-11
相关资源
最近更新 更多