Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2006
    Messaggi
    387

    Gestire le date in formato ISO

    Ciao....

    Su ASP ho sempre usato questa funzione:

    codice:
    function dateToString(theDate)
    sessionLCID = session.LCID
    	if not sessionLCID = 1040 then
    		session.LCID = 1040
    	end if
    	'if not isDate(theDate) then
    	'	theDate = now()
    	'end if
    		theYear = datePart("yyyy",theDate)
    		theMonth = datePart("m",theDate)
    		if theMonth < 10 then theMonth = "0" & theMonth
    		theDay = datePart("d",theDate)
    		if theDay < 10 then theDay = "0" & theDay
    		theHour = datePart("h",theDate)
    		if theHour < 10 then theHour = "0" & theHour
    		theMinutes = datePart("n",theDate)
    		if theMinutes < 10 then theMinutes = "0" & theMinutes
    		theSeconds = datePart("s",theDate)
    		if theSeconds < 10 then theSeconds = "0" & theSeconds
    		dateToString = theYear & theMonth & theDay & theHour & theMinutes & theSeconds
    		session.LCID = sessionLCID
    end function
    
    function stringToDate(theString)
    	if len(theString) = 14 then
    		theYear = left(theString,4)
    		theMonth = mid(theString,5,2)
    		theDay = mid(theString,7,2)
    		theHour = mid(theString,9,2)
    		theMinutes = mid(theString,11,2)
    		theSeconds = right(theString,2)
    			if session.LCID = 1040 then
    				stringToDate = theDay & "/" & theMonth & "/" & theYear
    				theChar = "."				
    			else
    				stringToDate = cInt(theMonth) & "/" & cInt(theDay) & "/" & theYear
    				theChar = ":"					
    			end if
    			stringToDate = cDate(stringToDate & " " & theHour & theChar & theMinutes & theChar & theSeconds)
    	else
    		stringToDate = now()
    	end if
    end function
    In soldoni, impostato il SessionLCID in italico, trasforma data e ora in un bel 20100603191221

    E poi si può risplittare al contrario...

    Il vantaggio è evidente!

    Ora... prima che mi ci metto e la riscrivo in PHPese ( ) non è che esiste qualcosa di già fatto da altri? Ho googlato per un po' ma non ho trovato molto... anzi.. niente....

    grazie!

  2. #2
    Utente di HTML.it
    Registrato dal
    Mar 2006
    Messaggi
    387
    Allora, andando avanti nello studio ho risolto la prima parte in questa maniera:

    codice:
    $data = date("d-m-Y");
    $vet = explode("-", $data); 
    $dataISO = $vet[2].$vet[1].$vet[0]; 
    echo $dataISO;
    I.e. stampo correttamente per oggi 20100307.

    Ora però vorrei fare il processo contrario.

    Non riesco però con i metodi (a me) noti a fare il reverse... Non riesco cioè a dirgli prendi le ultime due e salvale in $giorno, la 3 e 4 lettera in $mese e l'ultima in anno...

    Ringrazio in anticipo!

  3. #3
    Salva la data attuale (o quella che viene passata) in unix timestamp.

    codice:
    $data = time();
    
    // Converti $data (in valore unix timestamp) quando e come vuoi con:
    
    $dataISO = date('Ymd', $data);
    $dataNonISO = date('dmY', $data);

    Il silenzio è spesso la cosa migliore. Pensa ... è gratis.

  4. #4
    Utente di HTML.it
    Registrato dal
    Mar 2006
    Messaggi
    387
    Ho dimenticato di aggiornare...

    Alla fine ho risolto con queste due funzioncine:

    codice:
    function dataToISO($data)
    	{
    	
    	$vet = explode("-", $data); 
      	$dataISO = $vet[2].$vet[1].$vet[0];
    	return $dataISO;
    	
    	}
    	
    function ISOToData($dataISO)
    	{
    	$anno = substr($dataISO,0,4);
    	$mese = substr($dataISO,4,2);
    	$giorno = substr($dataISO,6,2);
    	$dataFromISO = $giorno."-".$mese."-".$anno;
    	return $dataFromISO;
    	}
    Grazien intanto per la risposta...

    Ho tralasciato il timestamp perchè, leggendo, ho letto che su windows si comporta in manisera a volte inaspettata.
    Volevo garantirmi una portabilità ottimale degli script.

    Sbaglio approccio?

  5. #5
    va tutto bene quel che risolve. Ma una domanda sorge dalle ceneri:

    dove prendi il valore che hai in $data?

    Il silenzio è spesso la cosa migliore. Pensa ... è gratis.

  6. #6
    Utente di HTML.it
    Registrato dal
    Mar 2006
    Messaggi
    387
    potrebbe essere automatico, come la data di inserimento di un post, oppure anche scelta da un calendario javascript...dipende dalle situazioni che mi si crederanno... Grazie x la risposta, buon weekend!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.