-1

I have

Fri Mar 13 2020 00:00:00 GMT+0100 (Ora standard dell’Europa centrale)-Mon Mar 23 2020 00:00:00 GMT+0100 (Ora standard dell’Europa centrale)

Expecting

Sardegna
13/03/2020-23/03/2020

But receiving:

Sardegna
12/03/2020-22/03/2020

Code

$count = 0;
foreach($dates as $arr){
    $exploded = explode('-',$arr);
    $finalTime = '';
    foreach($exploded as $explode){
        $timeString = trim(explode('(',$explode)[0]);
        $finalTime .= (!empty($finalTime)) ? "-".date('d/m/Y', strtotime($timeString)) : date('d/m/Y', strtotime($timeString));
    }
    echo "<li>".$nations[$count]."</li>";
    echo "<li>".$finalTime."</li>";
    echo PHP_EOL;
    $count++;
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
rob.m
  • 9,843
  • 19
  • 73
  • 162
  • 1
    If you look at the time as well, do you get `23:00:00` – RiggsFolly Mar 27 '20 at 15:52
  • @RiggsFolly I don't need the time, only the dates as per the question – rob.m Mar 27 '20 at 15:53
  • 3
    @rob.m A bit of a side note: please don't delete your question, even if you've found the solution. I spent 10 minutes detailing an answer to your question yesterday, only to have it deleted right before I could submit. If you've found the answer, simply leave the question and answer it yourself. It will help others and not waste people's time who are trying to help. – Dave Mar 27 '20 at 15:54
  • 1
    Ok, but check it, and then look at the `GMT+0100` and have a little think – RiggsFolly Mar 27 '20 at 15:54
  • If you change this line `$timeString = trim(explode('(',$explode)[0]);` to `$timeString = trim(explode(' GMT',$explode)[0]);` Does it work better – RiggsFolly Mar 27 '20 at 15:56
  • @Dave my apologies, had other moderator closing my questions because of their view on the quality, tried to tell them that people may help and understand what others don't and therefore I have deleted it to avoid closing the question. I had spent a lot of time in writing questions that got closed. Really sorry and thanks a lot for your help anyway. – rob.m Mar 27 '20 at 15:57
  • @Dave for example yesterday, look at this question, got closed even tho people were commenting and posting answer, not to mention the fact I was asking something else in respect of their duplicate link https://stackoverflow.com/questions/60865083/how-to-fix-dates-to-be-similar-format – rob.m Mar 27 '20 at 15:59
  • @rob.m getting closed by admins because it's a duplicate is different than closing it yourself because you found the answer. – Dave Mar 27 '20 at 16:01
  • Just leave a question there. If it shouldn't be there, someone will close it and point you in the right direction. The OP shouldn't close the question at all IMO, but especially if it's just because they figured out the answer. – Dave Mar 27 '20 at 16:07
  • @Dave I didn't close because I found my own answer. When I have my own answer, I place it myself without to close the question, see this from yesterday https://stackoverflow.com/a/60870809/1018804 – rob.m Mar 27 '20 at 16:09
  • 1
    They might be looking for you to have tried some solution(s) on your own. This one for instance, could have very-easily been debugged further to provide a simpler question. It does seem like you just hit a wall and posted your code in hopes someone would debug it for you. Next time, try to dig a little deeper and just supply the specific question "when I run X string through Y method, it returns Z, but I expected C". – Dave Mar 27 '20 at 16:35
  • 1
    My point is, had you debugged it down, you could have asked this question with a single line of code: "why isn't this date(...) working as expected. That's all. Just guessing as to why the downvote. I already said I thought it was an okay question. Just providing suggestion for next time. – Dave Mar 27 '20 at 18:02

2 Answers2

1

Change what you explode the string on from ( to ' GMT'

$count = 0;
foreach($dates as $arr){
    $exploded = explode('-',$arr);
    $finalTime = '';
    foreach($exploded as $explode){
        $timeString = trim(explode(' GMT',$explode)[0]);
        //                          ^^^^
        // or as you dont need the trim any more
        $timeString = explode(' GMT',$explode)[0];

        $finalTime .= (!empty($finalTime)) ? "-".date('d/m/Y', strtotime($timeString)) : date('d/m/Y', strtotime($timeString));
    }
    echo "<li>".$nations[$count]."</li>";
    echo "<li>".$finalTime."</li>";
    echo PHP_EOL;
    $count++;
}

This will remove the timezone, which is the reason for your hour difference

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

The quick/easy answer is to just strip the timezone to take that out of the equation:

// ...
$timeString = trim(explode('(',$explode)[0]); // (your code)
$timestring = str_replace("GMT+0100 (Ora standard dell’Europa centrale)", "", $timeString);
// ...

(This is just one of many not-super-elegant ways of removing the timezone. But it should prove the point)

Dave
  • 28,833
  • 23
  • 113
  • 183
  • so to put it in context should be `foreach($exploded as $explode){ $explode = str_replace("GMT+0100 (Ora standard dell’Europa centrale)", "");` correct? – rob.m Mar 27 '20 at 16:01
  • You can put it wherever you want. Just use this to take out the timezone prior to running it through `date()` method. – Dave Mar 27 '20 at 16:06
  • getting confused on "wherever you want", might you post an example in context please? – rob.m Mar 27 '20 at 16:08
  • mm getting 01/01/1970-01/01/1970 – rob.m Mar 27 '20 at 16:17