Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    Auto formattazione data

    Ciao a tutti, chiedo aiuto. Vorrei formattare la data di inserimento di un form da YYYY-MM-DD a DD/MM/YYYY per la selezione dati di un database da una certa data ad un'altra.
    Finchè uso l'inserimento YYYY-MM-DD funziona tutto correttamente ma non riesco con DD/MM/YYYY.
    Mi potete aiutare?
    Grazie mille



    <?php
    mysql_connect("localhost", "", "") or die(mysql_error());
    mysql_select_db("") or die(mysql_error());


    $data1=$_POST['da']; // sistema il name dell'input form mettendo l'underscore
    $data2=$_POST['a']; // underscore anche per questo


    //query
    $result=mysql_query("select date_format(date,'%d/%m/%Y')as giorno,MONTH(date) AS mese,avg(temp),avg(umidita),avg(20-(temp)) as gradigiorno from TABELLA2
    WHERE date BETWEEN '$data1' AND '$data2' group by giorno order by date desc limit 1000");


    // prepara intestazione tabella //
    echo "<table style='text-align:center'><tr ><th width='100px'>Giorno</th><th width='100px'>Temperatura</th><th width='100px'>Umidità</th><th

    width='100px'>Gradigiorno</th></tr>";

    while($row = mysql_fetch_assoc($result))

    {

    // crea riga tabella//
    echo "<tr><td >".($row['giorno'])."</td><td>".number_format(($row['avg(temp)']),2)."</td><td>".number_format(($row['avg

    (umidita)']),2)."</td><td>".number_format(($row['gradigiorno']),2)."</td></tr>";
    }

    // chiude tabella //
    echo "</table>";
    ?>

  2. #2
    E il form dove sarebbe?

  3. #3
    basta che usi l'istruzione date da qui puoi vedere che per scrivere una data nel formato italiano basta scrivere

    Codice PHP:
    $data date('G-M-Y'); 
    per tarsformare una certa data in tale formato

    Codice PHP:
    $data $row['data'];
    $arr explode('-'$data);
    $data date('G-M-Y'mktime(000$arr[1], $arr[2], $arr[0]); 
    guarda qui per
    mktime

  4. #4
    nel codice è possibile anche usare date_format?



    non capisco perchè cosi scritto mi accetta solo le date impostate con es. 2012-12-05
    e non con 05/12/2012
    forse sbaglio nel punto in cui richiamo la data con l'echo:

    echo "<tr><td >".($row['giorno']) etc....?

    Grazie mille
    qui di seguito la parte del codice:

    $result=mysql_query("select date_format(date,'%d/%m/%Y')as giorno,MONTH(date) AS mese,avg(temp),avg(umidita),avg(20-(temp)) as gradigiorno from TABELLA2
    WHERE date BETWEEN '$data1' AND '$data2' group by giorno order by date desc limit 1000");

    / prepara intestazione tabella //
    echo "<table style='text-align:center'><tr ><th width='100px'>Giorno</th><th width='100px'>Temperatura</th><th width='100px'>Umidità</th><th width='100px'>Gradigiorno</th></tr>";

    while($row = mysql_fetch_assoc($result))

    {

    // crea riga tabella//
    echo "<tr><td >".($row['giorno'])."</td><td>".number_format(($row['avg(temp)']),2)."</td><td>".number_format(($row['avg(umidita)']),2)."</td><td>".number_format(($row['gradigiorno']),2)."</td></tr>";
    }

  5. #5
    si puoi anche usare date_format, come puoi vedere qua in PHP e qua per MySQL.

    L'errore che tu fai però è nella query: non hai messo subito dopo il SELECT un cmapo da trattare come data e non è corretta nella forma e sintassi. Io ti consiglio di fare così: prima immetti la data da verificare
    Codice PHP:
    data (dd/mm/aaaa): <input type="text" name="data" /> 
    poi ricarichi la pagina e la converti in formato standard come ho scritto prima e scrivi la query

    Codice PHP:
    $data $_POST['data'];
    $arr explode('-'$data);
    $data date('Y-m-g');
    $query "select * FROM tabella2 WHERE data = '" $data "' AS giorno, SUBSTRING('" $data "', 6, 2) AS mese,";
    $query .= " avg(temp),avg(umidita),avg(20-(temp)) as gradigiorno from TABELLA2";
    $query .= " AND DATE('" $data "') BETWEEN '" $data1 "' AND '" $data2 "' GROUP BY giorno ORDER BY date DESC LIMIT 1000";
    $result=mysql_query($query); 

  6. #6

    GRAZIE

    grazie mille per la dritta

    ho risolto con la funzione:

    function data_us($data)
    {
    $array = explode("/", $data);
    $data_us = $array[2]."/".$array[1]."/".$array[0];
    return $data_us;
    }

  7. #7
    Utente di HTML.it L'avatar di garakkio
    Registrato dal
    Dec 2011
    residenza
    Roma
    Messaggi
    480
    ci sono già delle funzioni in PHP, apposta per gestire le date, senza doverne creare di inutili:

    Codice PHP:
    <?php
    echo date_format('d/m/Y'strtotime($data));

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.