Précédent   Forum des professionnels en informatique > PHP > Langage > Débuter
Débuter Forum d'entraide pour débuter en PHP. Avant de poster -> Cours PHP, FAQ PHP, Outils PHP, etc.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 24/04/2011, 23h39   #1
Invité de passage
 
Inscription : avril 2011
Messages : 5
Détails du profil
Informations forums :
Inscription : avril 2011
Messages : 5
Points : 0
Points : 0
Par défaut Soustraction de temps au millième de seconde

Bonjour a tous,

Voici mon problème:

J'ai deux valeurs temps dans une base de donnée SQLITE que je souhaiterais soustraire entre elles.

Ex:
$temps1=01:04.325
$temps2=01:04.830

$diff=$temps2-$temps

$diff=0.505

J'ai essayé TIMEDIFF, strftime mais je n'arrive à rien ou je ne sais pas correctement les utiliser.

* Autre question, comment cela se passe si le résultat est négatif? (cad temps2<temps1). Je risque de me retrouver de temps en temps dans ce cas de figure.

Merci par avance à ceux qui pourront m'aider
Vroomvroom est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 25/04/2011, 10h38   #2
Modérateur
 
Avatar de sabotage
 
Homme Vincent
Inscription : juillet 2005
Messages : 14 929
Détails du profil
Informations personnelles :
Nom : Homme Vincent

Informations forums :
Inscription : juillet 2005
Messages : 14 929
Points : 16 381
Points : 16 381
De la lecture :
http://www.sqlite.org/cvstrac/wiki?p...dTimeFunctions
sabotage est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 25/04/2011, 15h09   #3
Invité de passage
 
Inscription : avril 2011
Messages : 5
Détails du profil
Informations forums :
Inscription : avril 2011
Messages : 5
Points : 0
Points : 0
Merci pour ton lien.

Visiblement je n'arrive pas à utiliser la fonction strftime. Je n'obtiens aucun résultat.
Pourrais-tu me dire où j'ai fait une erreur?

Voici mon code simplifé:

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
 
// INFORMATIONS A SAISIR
$url = 'http://www.vola-racing.com/circuitpro/circuitalainprost/update.php?dir=circuitpro:circuitalainprost&index=0';
$data = utf8_decode(substr(file_get_contents($url), 8, -1));
 
$numero = '25';
 
// BASE DE DONNEE SQLITE
$doc = new DOMDocument();
$doc->loadHTML($data);
 
$xpath = new DOMXPath($doc);
 
try
{
    $dbh = new PDO("sqlite::memory:");
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
    exit($e->getMessage());
}
 
$dbh->query("CREATE TABLE race (clt INTEGER, num INTEGER, driver, lastlap TIME, interval TIME, lap INTEGER, blank)");
$stmt = $dbh->prepare("INSERT INTO race VALUES (?, ?, ?, ?, ?, ?, ?)");
 
foreach($xpath->query('//tr') as $line)
{
    $values = array();
 
    foreach ($xpath->query('td', $line) as $child)
    {        
        $values[] = $child->nodeValue;
    }
 
    $stmt->execute($values);
}
 
$query  = $dbh->query("SELECT * FROM `race` WHERE `num`='$numero' LIMIT 1");
$driver = $query->fetchObject();
 
// Notre pilote
$suivant = $driver->clt - 1;
$precedent = $driver->clt + 1;
 
// Pilote précédent
$query  = $dbh->query("SELECT * FROM `race` WHERE `clt`='$precedent' LIMIT 1");
$driverprecedent = $query->fetchObject();
 
// Pilote suivant
$query  = $dbh->query("SELECT * FROM `race` WHERE `clt`='$suivant' LIMIT 1");
$driversuivant = $query->fetchObject();
 
echo "dernier chrono notre pilote: ", $driver->lastlap, "<br>";
echo "dernier chrono pilote precedent: ",$driverprecedent->lastlap;
echo '<br>';
 
 
//Calcul de l'écart de chrono entre notre pilote et le pilote precedent/suivant
$diff= "SELECT strftime('%f','$driver->lastlap') - strftime('%f','$driverprecedent->lastlap')";
 
echo "difference de temps entre notre pilote et pilote precedent: ", $diff;
 
?>
Vroomvroom est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 25/04/2011, 17h39   #4
Modérateur
 
Avatar de sabotage
 
Homme Vincent
Inscription : juillet 2005
Messages : 14 929
Détails du profil
Informations personnelles :
Nom : Homme Vincent

Informations forums :
Inscription : juillet 2005
Messages : 14 929
Points : 16 381
Points : 16 381
$diff est ta chaine de caractère.
il manque l'execution et la lecture du résultat.
sabotage est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 26/04/2011, 20h52   #5
Invité de passage
 
Inscription : avril 2011
Messages : 5
Détails du profil
Informations forums :
Inscription : avril 2011
Messages : 5
Points : 0
Points : 0
Malheureusement je n'y arrive pas. Pourtant je cherche.

J'ai essayé ça mais aucun résultat.

Code :
1
2
$diff->queryExec ("SELECT strftime('%S.%f','$driver->lastlap') - strftime('%S.%f','$driverprecedent->lastlap')");
return $diff;
Pourrais-tu m'aider avec un code fonctionnel?
Vroomvroom est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/04/2011, 08h25   #6
Membre Expert
 
Avatar de transgohan
 
Homme Baptiste ROUSSEL
Étudiant
Inscription : janvier 2011
Messages : 802
Détails du profil
Informations personnelles :
Nom : Homme Baptiste ROUSSEL
Localisation : France, Territoire de Belfort (Franche Comté)

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : janvier 2011
Messages : 802
Points : 1 515
Points : 1 515
queryExec(), aurais-tu inventé une nouvelle fonction ?
La fonction est PDO::query(string) et elle te renvoie un objet PDOStatement.
Que tu peux parcourir avec PDOStatement::fetch() ou bien directement avec une boucle foreach().
transgohan est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/04/2011, 22h24   #7
Invité de passage
 
Inscription : avril 2011
Messages : 5
Détails du profil
Informations forums :
Inscription : avril 2011
Messages : 5
Points : 0
Points : 0
Code :
1
2
$diff = $dbh->query("SELECT strftime('%s','$driverprecedent->lastlap') - strftime('%s','$driver->lastlap')")->fetch();
echo $diff;
Cela ne marche toujours pas. J'ai juste un résultat: Array

Qu'est-ce qui ne va pas/ manque?

Merci d'aider un pauvre débutant...
Vroomvroom est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/04/2011, 23h16   #8
Modérateur
 
Avatar de sabotage
 
Homme Vincent
Inscription : juillet 2005
Messages : 14 929
Détails du profil
Informations personnelles :
Nom : Homme Vincent

Informations forums :
Inscription : juillet 2005
Messages : 14 929
Points : 16 381
Points : 16 381
sabotage est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/04/2011, 23h33   #9
Invité de passage
 
Inscription : avril 2011
Messages : 5
Détails du profil
Informations forums :
Inscription : avril 2011
Messages : 5
Points : 0
Points : 0
Malheureusement cela ne fonctionne pas. Je n'ai plus de résultat du tout.

Voici mon code complet si par hasard tu peux jeter un coup d'oeil

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
 
// INFORMATIONS A SAISIR
$url = 'http://www.vola-racing.com/circuitpro/circuitalainprost/update.php?dir=circuitpro:circuitalainprost&index=0';
$data = utf8_decode(substr(file_get_contents($url), 8, -1));
 
$numero = '25';
 
// BASE DE DONNEE SQLITE
$doc = new DOMDocument();
$doc->loadHTML($data);
 
$xpath = new DOMXPath($doc);
 
try
{
    $dbh = new PDO("sqlite::memory:");
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
    exit($e->getMessage());
}
 
$dbh->query("CREATE TABLE race (clt INTEGER, num INTEGER, driver, lastlap TIME, interval TIME, lap INTEGER, blank)");
$stmt = $dbh->prepare("INSERT INTO race VALUES (?, ?, ?, ?, ?, ?, ?)");
 
foreach($xpath->query('//tr') as $line)
{
    $values = array();
 
    foreach ($xpath->query('td', $line) as $child)
    {        
        $values[] = $child->nodeValue;
    }
 
    $stmt->execute($values);
}
 
$query  = $dbh->query("SELECT * FROM `race` WHERE `num`='$numero' LIMIT 1");
$driver = $query->fetchObject();
 
// Notre pilote
$suivant = $driver->clt - 1;
$precedent = $driver->clt + 1;
 
// Pilote précédent
$query  = $dbh->query("SELECT * FROM `race` WHERE `clt`='$precedent' LIMIT 1");
$driverprecedent = $query->fetchObject();
 
// Pilote suivant
$query  = $dbh->query("SELECT * FROM `race` WHERE `clt`='$suivant' LIMIT 1");
$driversuivant = $query->fetchObject();
 
echo "dernier chrono notre pilote: ", $driver->lastlap, "<br>";
echo "dernier chrono pilote precedent: ",$driverprecedent->lastlap;
echo '<br>';
 
 
//Calcul de l'écart de chrono entre notre pilote et le pilote precedent/suivant
 
$diff = $dbh->query("SELECT strftime('%s','$driverprecedent->lastlap') - strftime('%s','$driver->lastlap')")->fetch();
echo $diff[0];
 
?>
Vroomvroom est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/04/2011, 08h18   #10
Membre Expert
 
Avatar de transgohan
 
Homme Baptiste ROUSSEL
Étudiant
Inscription : janvier 2011
Messages : 802
Détails du profil
Informations personnelles :
Nom : Homme Baptiste ROUSSEL
Localisation : France, Territoire de Belfort (Franche Comté)

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : janvier 2011
Messages : 802
Points : 1 515
Points : 1 515
Que donne :
transgohan est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 06h47.


 
 
 
 
Partenaires

Hébergement Web