RES: [PHP] From 24/7/2013 to 2013-07-24

2013-07-26 Thread Alejandro Michelin Salomon
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:jomali3...@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
wrote:

> On 13-07-26 11:42 AM, jomali wrote:
>
>> On Fri, Jul 26, 2013 at 5:18 AM, Karl-Arne Gjersøyen 
>> > >wrote:
>>
>>  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);
>>>
>>> What I want is a way to rewrite contents of a variable like this:
>>>
>>>  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:
>
> 
> $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



RES: [PHP] From 24/7/2013 to 2013-07-24

2013-07-26 Thread Alejandro Michelin Salomon
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:jim.gi...@albanyhandball.com] 
Enviada em: sexta-feira, 26 de julho de 2013 11:12
Para: php-general@lists.php.net
Assunto: Re: [PHP] From 24/7/2013 to 2013-07-24

On 7/26/2013 10:10 AM, Jim Giner wrote:
> 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