Discussion:
From 24/7/2013 to 2013-07-24
Karl-Arne Gjersøyen
2013-07-26 09:18:03 UTC
Permalink
Below is something I try that ofcourse not work because of rsosort.
Here is my code:
-----------------------
$lagret_dato = $_POST['lagret_dato'];
foreach($lagret_dato as $dag){

$dag = explode("/", $dag);
rsort($dag);
$dag = implode("-", $dag);
var_dump($dag);
From 24/7/2013 to 2013-07-24
Is there a way in PHP to do this?

Thank you very much.

Karl
Přemysl Fiala
2013-07-26 09:20:36 UTC
Permalink
Hello,

try & - reference

foreach($lagret_dato as &$dag) or something like this :-)

Premek.

On Fri, 26 Jul 2013 11:18:03 +0200, Karl-Arne Gjers=C3=B8yen =
Post by Karl-Arne Gjersøyen
Below is something I try that ofcourse not work because of rsosort.
-----------------------
$lagret_dato =3D $_POST['lagret_dato'];
foreach($lagret_dato as $dag){
$dag =3D explode("/", $dag);
rsort($dag);
$dag =3D implode("-", $dag);
var_dump($dag);
From 24/7/2013 to 2013-07-24
Is there a way in PHP to do this?
Thank you very much.
Karl
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Karl-Arne Gjersøyen
2013-07-26 10:21:35 UTC
Permalink
$foo = DateTime::createFromFormat('d/m/Y');
$newDate = $foo->format('Y-m-d');
Thank you veyr much. With a small modification this work perfec!

Karl
Below is something I try that ofcourse not work because of rsosort.
Post by Karl-Arne Gjersøyen
-----------------------
$lagret_dato = $_POST['lagret_dato'];
foreach($lagret_dato as $dag){
$dag = explode("/", $dag);
rsort($dag);
$dag = implode("-", $dag);
var_dump($dag);
From 24/7/2013 to 2013-07-24
Is there a way in PHP to do this?
Thank you very much.
Karl
Jim Giner
2013-07-26 14:10:10 UTC
Permalink
I think you should change from using 'rsort' ( a SORT function) to
'array_reverse', a simple reverse function.
Your example of what you desire is wrong.
24-7-2013 will give you the 2013-24-7 that you want.
Here is my sample code. Try it yourself.

<?
$dag = array("24/7/2013");
echo "Began with: ";var_dump( $dag);
echo "<br>**********<br>";
echo "Try using rsort<br>";
$dagparts = explode("/",$dag[0]);
echo "dagparts: ";
var_dump($dagparts);
echo "<br>**********<br>";
rsort($dagparts);
echo "sorted dagparts: ";
var_dump($dagparts);
echo "<br>**********<br>";
$newdag = implode("-",$dagparts);
echo "newdag: ";
var_dump($newdag);
echo "<br>**********<br>";
echo "Now use array_reverse<br>";
$dagparts = explode("/",$dag[0]);
echo "dagparts: ";
var_dump($dagparts);
echo "<br>**********<br>";
$dagparts = array_reverse($dagparts);
echo "REVERSED dagparts: ";
var_dump($dagparts);
echo "<br>**********<br>";
$newdag = implode("-",$dagparts);
echo "newdag: ";
var_dump($newdag);
echo "<br>**********<br>";
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2013-07-26 14:12:07 UTC
Permalink
Post by Jim Giner
I think you should change from using 'rsort' ( a SORT function) to
'array_reverse', a simple reverse function.
Your example of what you desire is wrong.
24-7-2013 will give you the 2013-24-7 that you want.
oops.
I meant to say "will NOT give you the 2013-2407 that you want".
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Alejandro Michelin Salomon
2013-07-26 15:06:42 UTC
Permalink
Use this:

echo preg_replace('#(\d{2})/(\d{2})/(\d{4})#' , "\\3-\\2-\\1", '24/07/2013'
); RESULT => 2013-07-24

Alejandro M.S


-----Mensagem original-----
De: Jim Giner [mailto:***@albanyhandball.com]
Enviada em: sexta-feira, 26 de julho de 2013 11:12
Para: php-***@lists.php.net
Assunto: Re: [PHP] From 24/7/2013 to 2013-07-24
Post by Jim Giner
I think you should change from using 'rsort' ( a SORT function) to
'array_reverse', a simple reverse function.
Your example of what you desire is wrong.
24-7-2013 will give you the 2013-24-7 that you want.
oops.
I meant to say "will NOT give you the 2013-2407 that you want".


--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
jomali
2013-07-26 15:42:50 UTC
Permalink
Post by Karl-Arne Gjersøyen
Below is something I try that ofcourse not work because of rsosort.
-----------------------
$lagret_dato = $_POST['lagret_dato'];
foreach($lagret_dato as $dag){
$dag = explode("/", $dag);
rsort($dag);
$dag = implode("-", $dag);
var_dump($dag);
From 24/7/2013 to 2013-07-24
Is there a way in PHP to do this?
Thank you very much.
Karl
$conv_date = str_replace('/', '-','24/7/2013');
echo date('Y-m-d', strtotime($conv_date));
Result: 2013-07-24
Robert Cummings
2013-07-26 17:08:44 UTC
Permalink
Post by jomali
Post by Karl-Arne Gjersøyen
Below is something I try that ofcourse not work because of rsosort.
-----------------------
$lagret_dato = $_POST['lagret_dato'];
foreach($lagret_dato as $dag){
$dag = explode("/", $dag);
rsort($dag);
$dag = implode("-", $dag);
var_dump($dag);
From 24/7/2013 to 2013-07-24
Is there a way in PHP to do this?
Thank you very much.
Karl
$conv_date = str_replace('/', '-','24/7/2013');
echo date('Y-m-d', strtotime($conv_date));
Result: 2013-07-24
It would be better if you reformatted first since this is ambiguous when
you have the following date:

6/7/2013

Here's a completely unambiguous solution:

<?php

$old = '24/7/2013';

$paddy = function( $bit ){ return str_pad( $bit, 2, '0',
STR_PAD_LEFT ); };
$new = implode( '-', array_map( $paddy, array_reverse( explode(
'/', $old ) ) ) );

echo $new."\n";

?>

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
jomali
2013-07-26 20:38:07 UTC
Permalink
Post by Robert Cummings
Post by Karl-Arne Gjersøyen
Below is something I try that ofcourse not work because of rsosort.
Post by Karl-Arne Gjersøyen
-----------------------
$lagret_dato = $_POST['lagret_dato'];
foreach($lagret_dato as $dag){
$dag = explode("/", $dag);
rsort($dag);
$dag = implode("-", $dag);
var_dump($dag);
From 24/7/2013 to 2013-07-24
Is there a way in PHP to do this?
Thank you very much.
Karl
$conv_date = str_replace('/', '-','24/7/2013');
echo date('Y-m-d', strtotime($conv_date));
Result: 2013-07-24
It would be better if you reformatted first since this is ambiguous when
6/7/2013
<?php
$old = '24/7/2013';
$paddy = function( $bit ){ return str_pad( $bit, 2, '0', STR_PAD_LEFT
); };
$new = implode( '-', array_map( $paddy, array_reverse( explode( '/',
$old ) ) ) );
echo $new."\n";
?>
Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
The original question was about reformatting a European (Day/Month/Year)
date. Your solution does not address this problem. Mine assumes the
European date format explicitly.
Alejandro Michelin Salomon
2013-07-26 20:44:14 UTC
Permalink
jomali:

Use this:

echo preg_replace('#(\d{2})/(\d{2})/(\d{4})#' , "\\3-\\2-\\1", '24/07/2013'
); RESULT => 2013-07-24

Alejandro M.S
-----Mensagem original-----
De: jomali [mailto:***@gmail.com]
Enviada em: sexta-feira, 26 de julho de 2013 17:38
Para: Robert Cummings
Cc: Karl-Arne Gjersøyen; PHP Mailinglist
Assunto: Re: [PHP] From 24/7/2013 to 2013-07-24

On Fri, Jul 26, 2013 at 1:08 PM, Robert Cummings
Post by Robert Cummings
On Fri, Jul 26, 2013 at 5:18 AM, Karl-Arne Gjersøyen
Below is something I try that ofcourse not work because of rsosort.
Post by Karl-Arne Gjersøyen
-----------------------
$lagret_dato = $_POST['lagret_dato'];
foreach($lagret_dato as $dag){
$dag = explode("/", $dag);
rsort($dag);
$dag = implode("-", $dag);
var_dump($dag);
From 24/7/2013 to 2013-07-24
Is there a way in PHP to do this?
Thank you very much.
Karl
$conv_date = str_replace('/', '-','24/7/2013'); echo date('Y-m-d',
strtotime($conv_date));
Result: 2013-07-24
It would be better if you reformatted first since this is ambiguous
6/7/2013
<?php
$old = '24/7/2013';
$paddy = function( $bit ){ return str_pad( $bit, 2, '0',
STR_PAD_LEFT ); };
$new = implode( '-', array_map( $paddy, array_reverse( explode(
'/', $old ) ) ) );
echo $new."\n";
?>
Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
The original question was about reformatting a European (Day/Month/Year)
date. Your solution does not address this problem. Mine assumes the European
date format explicitly.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Cummings
2013-07-26 21:29:41 UTC
Permalink
Post by jomali
Post by Robert Cummings
Post by Karl-Arne Gjersøyen
Below is something I try that ofcourse not work because of rsosort.
Post by Karl-Arne Gjersøyen
-----------------------
$lagret_dato = $_POST['lagret_dato'];
foreach($lagret_dato as $dag){
$dag = explode("/", $dag);
rsort($dag);
$dag = implode("-", $dag);
var_dump($dag);
From 24/7/2013 to 2013-07-24
Is there a way in PHP to do this?
Thank you very much.
Karl
$conv_date = str_replace('/', '-','24/7/2013');
echo date('Y-m-d', strtotime($conv_date));
Result: 2013-07-24
It would be better if you reformatted first since this is ambiguous when
6/7/2013
<?php
$old = '24/7/2013';
$paddy = function( $bit ){ return str_pad( $bit, 2, '0', STR_PAD_LEFT
); };
$new = implode( '-', array_map( $paddy, array_reverse( explode( '/',
$old ) ) ) );
echo $new."\n";
?>
Cheers,
Rob.
The original question was about reformatting a European (Day/Month/Year)
date. Your solution does not address this problem. Mine assumes the
European date format explicitly.
Jomali,
Post by jomali
Post by Robert Cummings
Post by Karl-Arne Gjersøyen
Post by Karl-Arne Gjersøyen
From 24/7/2013 to 2013-07-24
Your solution makes use of the strtodate(). A useful strategy EXCEPT (as
you have already noted) the date follows the European formatting rules
of dd/mm/yyyy since the 24 as the first number makes that obvious.
HOWEVER, you failed to realize the following (from the PHP online manual):

Dates in the m/d/y or d-m-y formats are disambiguated by looking
at the separator between the various components: if the separator
is a slash (/), then the American m/d/y is assumed; whereas if
the separator is a dash (-) or a dot (.), then the European d-m-y
format is assumed.

And so, as soon as an abiguous date arises, the solution will be
incorrect because strtotime() will presume an American format due to the
appearance of the slash instead of the hyphen. It is dangerous to rely
on magical functions like strtotime() unless you completely understand
how ambiguity is resolved.

Another solution that was posted only re-ordered the elements and you
likely noticed that there is a single digit 7 in the source date and in
the response date it has been 0 padded to conform to the standard
yyyy-mm-dd date format. The other solution does not do this and so it is
also incorrect.

And so it follows, that my solution, thus far, is the only solution
posted that actually meets the requirements. Why you think my solution
does not perform is beyond me since a simple run of the code would
output the correct answer (yes I did test)-- mine also presumes the
European ordering for all input with components separated by a slash.

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2013-07-26 21:33:17 UTC
Permalink
Post by Robert Cummings
Post by Alejandro Michelin Salomon
On Fri, Jul 26, 2013 at 1:08 PM, Robert Cummings
Post by Robert Cummings
On Fri, Jul 26, 2013 at 5:18 AM, Karl-Arne Gjersøyen
Below is something I try that ofcourse not work because of rsosort.
Post by Karl-Arne Gjersøyen
-----------------------
$lagret_dato = $_POST['lagret_dato'];
foreach($lagret_dato as $dag){
$dag = explode("/", $dag);
rsort($dag);
$dag = implode("-", $dag);
var_dump($dag);
From 24/7/2013 to 2013-07-24
Is there a way in PHP to do this?
Thank you very much.
Karl
$conv_date = str_replace('/', '-','24/7/2013');
echo date('Y-m-d', strtotime($conv_date));
Result: 2013-07-24
It would be better if you reformatted first since this is ambiguous when
6/7/2013
<?php
$old = '24/7/2013';
$paddy = function( $bit ){ return str_pad( $bit, 2, '0', STR_PAD_LEFT
); };
$new = implode( '-', array_map( $paddy, array_reverse( explode( '/',
$old ) ) ) );
echo $new."\n";
?>
Cheers,
Rob.
The original question was about reformatting a European (Day/Month/Year)
date. Your solution does not address this problem. Mine assumes the
European date format explicitly.
Jomali,
Post by Alejandro Michelin Salomon
Post by Robert Cummings
Post by Karl-Arne Gjersøyen
From 24/7/2013 to 2013-07-24
Your solution makes use of the strtodate(). A useful strategy EXCEPT (as
you have already noted) the date follows the European formatting rules
of dd/mm/yyyy since the 24 as the first number makes that obvious.
Dates in the m/d/y or d-m-y formats are disambiguated by looking
at the separator between the various components: if the separator
is a slash (/), then the American m/d/y is assumed; whereas if
the separator is a dash (-) or a dot (.), then the European d-m-y
format is assumed.
And so, as soon as an abiguous date arises, the solution will be
incorrect because strtotime() will presume an American format due to the
appearance of the slash instead of the hyphen. It is dangerous to rely
on magical functions like strtotime() unless you completely understand
how ambiguity is resolved.
Another solution that was posted only re-ordered the elements and you
likely noticed that there is a single digit 7 in the source date and in
the response date it has been 0 padded to conform to the standard
yyyy-mm-dd date format. The other solution does not do this and so it is
also incorrect.
And so it follows, that my solution, thus far, is the only solution
posted that actually meets the requirements. Why you think my solution
does not perform is beyond me since a simple run of the code would
output the correct answer (yes I did test)-- mine also presumes the
European ordering for all input with components separated by a slash.
Cheers,
Rob.
And my solution doesn't work?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Cummings
2013-07-26 22:38:20 UTC
Permalink
Post by Jim Giner
Post by Robert Cummings
And so it follows, that my solution, thus far, is the only solution
posted that actually meets the requirements. Why you think my solution
does not perform is beyond me since a simple run of the code would
output the correct answer (yes I did test)-- mine also presumes the
European ordering for all input with components separated by a slash.
Cheers,
Rob.
And my solution doesn't work?
I don't see any padding happening in your solution. Your solution produced:

2013-7-24

The required solution is:

2013-07-24

:)

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tamara Temple
2013-07-27 01:33:05 UTC
Permalink
Post by Karl-Arne Gjersøyen
Below is something I try that ofcourse not work because of rsosort.
-----------------------
$lagret_dato = $_POST['lagret_dato'];
foreach($lagret_dato as $dag){
$dag = explode("/", $dag);
rsort($dag);
You want to reverse the array, not sort it :) array_reverse().
Post by Karl-Arne Gjersøyen
$dag = implode("-", $dag);
var_dump($dag);
From 24/7/2013 to 2013-07-24
Is there a way in PHP to do this?
Thank you very much.
Karl
Also, I thought the locale for European countries used periods as separators, not slashes; slashes for North America…

*shrug*
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tamara Temple
2013-07-27 01:35:12 UTC
Permalink
augh, apologies; i didn't see all the other replies….
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...