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.

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

报告相同问题?

悬赏问题

  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆