-3

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
  • *"Thank you very much in advance."* - what does that mean? – Funk Forty Niner Apr 02 '18 at 16:41
  • Please provide sample data from `$press_date` and **validate** the Array-Entry with `isset($news_date[1])` for sample! – Adrian Preuss Apr 02 '18 at 16:42
  • @AdrianPreuss this is an sample data: 2014-07-21 – user9111600 Apr 02 '18 at 16:43
  • it means there is no data in $news_date[1]. this can be due to $press_date is empty or $press_date has one string without '-' like $press_date('10') without any '-'. check $press_date – prasanna puttaswamy Apr 02 '18 at 16:44
  • Did you know PHP has various functions specifically for handling dates? – Don't Panic Apr 02 '18 at 16:45
  • `DateTime()` class would be useful here – Rotimi Apr 02 '18 at 16:46
  • @Don'tPanic True, but simplicity is sometimes efficient as well :) Besides, its a learning curve. – Xorifelse Apr 02 '18 at 16:46
  • 1
    If an answer solved your problem, consider accepting the answer. Here's how http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work then return here and do the same with the tick/checkmark till it turns green. This informs the community, a solution was found. Otherwise, others may think the question is still open and may want to post (more) answers. You'll earn points and others will be encouraged to help you. *Welcome to Stack!* – Jay Blanchard Apr 02 '18 at 17:26
  • @JayBlanchard Thank you! I did it already. Sorry if my question make the Stack like spam! But I don't know how I start to research. Anyway, I know now and my proble is solved. – user9111600 Apr 02 '18 at 17:32

1 Answers1

0

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.

Adrian Preuss
  • 3,228
  • 1
  • 23
  • 43
  • Many thanks Adrian! This is my fault. Now I am using DateTime() to show the data. ANyway, Your suggestion is help me too much. So sorry to cannot click your comment to "USEFUL". – user9111600 Apr 02 '18 at 17:18