Calculate time difference with 2 or more fields

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
Stephan
Posts: 2
Joined: 2015-06-17 06:39

Calculate time difference with 2 or more fields

Post by Stephan » 2015-06-17 11:15

Hi everybody,
I'm sorry for my english... i'm french... :roll:
I have 2 problem with hook :
i want to calculate in time, one field.
I have 1 table with 3 fields like this:
- heure_debut
- heure_fin
- duree
the result field is 'duree'
in the hook file 'cours.php' under the function cours_after_insert($data, $memberInfo, &$args) i'm write this, but i dont have any result ?
$to_time = strtotime($data['heure_fin']);
$from_time = strtotime($data['heure_debut']);
$data['duree'] = round(abs($to_time - $from_time) / 60,2). " minutes";
all the fields are in time format
can i have some help ?
Thank

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Calculate time difference with 2 or more fields

Post by a.gneady » 2015-06-17 21:15

You should put your code in the before_insert hook rather than after_insert. Or, you can keep it in after_insert, but use a manual update query after your code.
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

Stephan
Posts: 2
Joined: 2015-06-17 06:39

Re: Calculate time difference with 2 or more fields

Post by Stephan » 2015-06-18 06:39

Thank for your answer...
i tried to put in the before and the after but nothing....
I made a function in the hook :
function duree_init($data){
$duree = makeSafe($data['duree']);
/*
/*
retrieve the ID of the courses to be updated, and make it safe for use in SQL
*/
$coursid = makeSafe($data['cours_id']);
/*
retrieve the ID of table to be updated, and make it safe for use in SQL
*/
$cours = makeSafe($data['cours']);
/*
Put other item in variable and make it safe for use in SQL
*/
$debut = makeSafe($data['heure_debut']);
$fin = makeSafe($data['heure_fin']);

/* calculating time */
if($duree){

$calcul_duree = sqlValue("SELECT SUBTIME(`heure_fin`,`heure_debut`) FROM `cours`");

/* update the item with the value calculated above */
sql("UPDATE '$cours' SET '$duree'='$calcul_duree' where '$coursid'");
}
}
and I call it in the before and after but i don't have any result ??
function cours_before_insert(&$data, $memberInfo, &$args){
duree_init($data);
return TRUE;
}
When i do the same function in phpmyadmin, it's working :
UPDATE `cours` SET `duree`=SUBTIME(`heure_fin`,`heure_debut`) WHERE *
i'm lost...
Thank for your help, i'm newbies on appgini

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Calculate time difference with 2 or more fields

Post by a.gneady » 2015-06-18 20:51

That query

Code: Select all

SELECT SUBTIME(`heure_fin`,`heure_debut`) FROM `cours`
needs a where clause .. otherwise you won't be sure which record the result is being returned from. So try

Code: Select all

SELECT SUBTIME(`heure_fin`,`heure_debut`) FROM `cours` WHERE cours_id='$coursid'
Also, the where clause in the second query should be a correct expression:

Code: Select all

where cours_id='$coursid'
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

Post Reply