douqie1816 2018-04-02 16:41
浏览 63
已采纳

PHP注意:未定义的偏移量:1,带有日期字段的字符串[重复]

I have got this issue:

Notice: Undefined offset: 1 in every row of $news_date[1].

Here are my code:

$news_date=explode("-",$press_date);
    if($news_date[1]==1){
    $news_date[1]='JAN';
    }else if($news_date[1]==2){
    $news_date[1]='FEB';
    } //and so on..
$press_date=$news_date[1].', '.$news_date[0];

I stored date data with news_date field.

Please kindly suggestion How can I should do.

Thank you very much in advance.

*** Editable**** After ask via Stack now I have used these code to solve my problem. It is just using 3 lines. Thank you Adrian Preuss for your suggestion.

    $press_date=$rs['news_date']; // store data from DB
    $new_formatDate=date("M d, Y",strtotime($press_date));  // Set Date Month Year styles that I want
    $new_formatDate = strtoupper($new_formatDate);  // Make it to ALL CAP
</div>
  • 写回答

1条回答 默认 最新

  • donglu5612 2018-04-02 16:55
    关注

    You wan't to work with Date formats- Your problem is, you split the Date simply with explode - This way is not correct, because your splitted data has following format:

    [0] "2014"
    [1] "07"
    [2] "21"
    

    You check the splitted string (after splitting, each entry is also an string to) with integers:

    if("07" == 1) {
    

    The second problem is, you don't validate, if the data is set. The next problem is: You want to display only another date format - Please use PHP-based functions for formatting. Here are a sample for you:

    <?php
    $press_date = '2014-07-21';
    
    print date('d. M Y', strtotime($press_date));
    

    Output: 21. Jul 2014

    <?php
    $press_date = '2014-07-21';
    
    print date('l, d. F Y', strtotime($press_date));
    

    Output: Monday, 21. July 2014

    Informations about the output format can be found at http://php.net/manual/de/function.date.php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?