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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵