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.

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

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题