Jump to content

trying to loop a code - change date format from DD.MM.YY to YYYY/MM/DD


superinterstellar
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi,

I am trying to loop a code(my php skills are not that great, so appreciate if someone here could help me walk through how to loop my code)

 

 

I have date input in the format as 21.03.15(DD.MM.YYYY) and would like to have it in 2015/03/21(YYYY/MM/DD) .

 

Note: I have a html textform that will get all the dates added to it , it will be in the following format:-

21.03.15

22.03.15

22.03.15

 

I would like it to come out as :

 

2015/03/21

2015/03/22

2015/03/22

 

Below is some things I came up with, but I cannot seem to come up with a looping logic. Appreciate if someone here could help me with the code below.

Thanks!

<?php

$var = "21.03.15" ;

echo $var;
echo "<br />";


$date = explode(".",$var);  // 
echo "20".$date[2].'/'.$date[1].'/'.$date[0];    


?>
Link to comment
Share on other sites

the reason why i want to loop it because on the html form it will have all the dates coming in a straight line.

 

The html input(textarea) will go in like this:-

...

21.03.15

22.03.15

22.04.15

25.04.15

...

 

When the results are printed I need a break tag for each date that is printed out,

 

2015-03-21

2015-03-22

2015-04-22

2015-05-25

 

 

Hope this makes sense?

 

Thanks

Kevin

Link to comment
Share on other sites

PHP has date/datetime classes. They literally allow for any type of format you want. I'd suggest you start with those because they also provide the ability to do date arithmetic.

 

With that said, there seems to be no rhyme or reason behind your examples. What are these dates and where do they come from?

 

There is no way to show you a loop when we don't know what you're looping through!

Link to comment
Share on other sites

@gizmola

 

These are inputs from a html textarea input for dates in dd.mm.yy format. (It allows you to list all the dates you wish to convert. You have to list them all) . T

The reason why I am making this is because I have a colleague who has listed all his dates in excel in dd.mm.yyyy format and I want to convert them all into yyyy-mm-dd by copying all the dates and pasting them into the html textarea.

 

Then the php script will take all the dates listed and covert them one by one into YYYY-MM-DD format. After each date is converted it will have a break tag so that all the dates look in 1 orderly line.

 

I hope this makes more sense?

Link to comment
Share on other sites

  • Solution

When get the contents of the textarea you would use explode on the newline whitespace character to the dates on each line. You would then use foreach to loop over each date converting them using Barands example code

<?php

// if dates have been submitted
if(isset($_POST['dates']))
{
    // explode on the newline character to get the dates on each line
    $htmldates = explode("\n", $_POST['dates']);
    // loop through the dates
    foreach($htmldates as $htmldate)
    {
        // apply trim, to remove any whitespace
        $htmldate = trim($htmldate);
        // convert date to new format
        $dt = DateTime::createFromFormat('d.m.y', $htmldate);
        echo $dt->format('Y-m-d') . '<br />';
    }
}


?>

<form method="post">
    Dates:<br />
    <textarea name="dates" cols="30" rows="10"><?php echo (isset($_POST['dates']) ? $_POST['dates'] : ''); ?></textarea><br />
    <input type="submit" value="Convert" />
</form>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.