1

I am receiving 2 date formats:

1/22/2020

1/22/20

I need to convert 1/22/2020 to 1/22/20

I currently use the following but I am not sure how to do a check and convert it before to add my dates

    foreach($header as $field){
        if (preg_match("/([1-9]|1[0-2])\/([1-9]|[1-2][0-9]|3[0-1])\/[0-9]{2}$/",$field)) {

            if($insert){
                $days[] = ["date" => $field, $type => $value[$field]];
            }
            else{
                $date_index = array_search($field, array_column($globalData[$index]['data'], 'date'));

                $globalData[$index]['data'][$date_index][$type] = $value[$field];
            }
        }
    }
rob.m
  • 9,843
  • 19
  • 73
  • 162
  • 1
    why a duplicate? I am not asking how to convert a date. – rob.m Mar 26 '20 at 11:51
  • I agree that the "duplicate" does nothing to turn a 4 digit year into a 2 digit year. All it shows is how to move the year from the front to the end. That said, your question is unclear. "how to do a check and convert it". Convert what? Check what? And what version do you wnat to keep: `20` or `2020`? Please give an example of what you expect vs what you get. What does your code currently do? What about if you `split` on `/`, then if the 3rd element is < 100, add either 1900 or 2000 to get the appropriate year, then `join`? – SherylHohman Apr 01 '20 at 19:40
  • I voted for reopening, but actually you did not make it clear which version you actually need. And the code you provided seems irrelevant to your actual question. What code did you try in order to determine if the "date" needs modified, and if it does, where is your code for converting it to the other format. In what way did your code not work. Without the additional info, and *relevant* code (your actual attempt). I nominate this question for *closing* Not as duplicate, but as needing more info (what format do you want), or off topic (where is your attempt)? @OtoShavadez answer looks good. – SherylHohman Apr 01 '20 at 20:00
  • @SherylHohman this `regex "/([1-9]|1[0-2])\/([1-9]|[1-2][0-9]|3[0-1])\/[0-9]{2}$/"` checks for 2 digits. I do say I need to `convert 1/22/2020 to 1/22/20` so I did specify the format. I am asking how to convert it and provided the way I do read that date. – rob.m Apr 01 '20 at 22:25

2 Answers2

2

You can use php DateTime::createFromFormat function for converting the date in more robust way.

// filter only 4 digit year using regex
if (preg_match('/^\d{1,2}\/\d{1,2}\/\d{4}$/', $date)) {
    $dateObj = DateTime::createFromFormat('n/d/Y', $date);
    if ($dateObj === false) {
      throw new Exception("Not a valid date");
    }
    // convert it to 2 digit year
    echo $dateObj->format('n/j/y');
}

Here's how it can apply to your problem

<?php
    foreach($header as $field){
        if (preg_match('/^\d{1,2}\/\d{1,2}\/\d{4}$/', $field)) {
            $dateObj = DateTime::createFromFormat('n/d/Y', $field);

            if ($dateObj === false) {
                throw new Exception("Not a valid date");
            }
            // convert it to 2 digit year
            $field = $dateObj->format('n/j/y');

            if($insert){
                $days[] = ["date" => $field, $type => $value[$field]];
            }
            else{
                $date_index = array_search($field, array_column($globalData[$index]['data'], 'date'));

                $globalData[$index]['data'][$date_index][$type] = $value[$field];
            }
        }
    }
Gihan
  • 4,163
  • 6
  • 31
  • 49
  • ok this looks like it, mind to apply it to code on the question please? – rob.m Mar 26 '20 at 10:58
  • I just updated the answer. But there are many unknown variables in your sample code. So, please tests. – Gihan Mar 26 '20 at 11:11
  • Yes you're right, thanks. It is breaking indeed, basically grabbing 3 csv, they have the same format yet one has 4 year digits while we need with 2. The combining them into a single json file, here a paste bin of full code if it helps https://pastebin.com/AS7gBBsY – rob.m Mar 26 '20 at 11:15
1

Do you need this:

$str1 = "1/22/2024";
$str2 = "1/22/24";



function get_needed_format($str) {
    if(strlen(explode("/", $str)[2]) === 4) {
        return substr($str, 0, -4).substr($str, -2);
    }

    return $str;
}


echo get_needed_format($str1);
echo "\n";
echo get_needed_format($str2);

?

Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236
  • I need to do a check before I add them to my array, I am receiving 2 formats so first check if they're /20 or /2020 and then add – rob.m Mar 26 '20 at 10:39
  • Edited, so if you have `1/22/2024` and `1/22/24`, both gives: `1/22/24`. Is that correct? – Oto Shavadze Mar 26 '20 at 10:44
  • yeah well, needs to be applied to my code in the question tho, here's a pastebin of the full code it helps, basically grabbing 3 csvs, https://pastebin.com/AS7gBBsY – rob.m Mar 26 '20 at 11:16
  • 1
    @rob.m, they gave you the missing logic to your issue, cannot you figure out how to apply it to the foreign code? Or is your actual question that you don't understand what the given code is doing? Honestly, it seems that the code you gave is actually irrelevant to the question asked, and not our problem to figure out. That code and your question seem to have a disconnect. +1 to Oto Shavadze. Also, the question does not state whether the 2-digit or the 4-digit is needed. The 4-digit case is a tad bit more difficult because 1900 vs 2000. Since it's not stated, this easier version is perfect. – SherylHohman Apr 01 '20 at 19:54
  • @SherylHohman this `regex "/([1-9]|1[0-2])\/([1-9]|[1-2][0-9]|3[0-1])\/[0-9]{2}$/"` checks for 2 digits. I do say I need to `convert 1/22/2020 to 1/22/20` so I did specify the format. I am asking how to convert it and provided the way I do read that date. – rob.m Apr 01 '20 at 22:25