Jump to content

timestamp script weird problem


Miko

Recommended Posts

Hi!

 

To calculate my real time hours worked for/at a client I've made 3scripts, 1 to start the time, 1 to show the running time and 1 for stopping it.

The first and last are put into my hosted DB. I'm accessing them through SSH so that I don't need my laptop all the time.

 

Now here's the problem, when I start the time and I look into my DB I see the actual correct time and date (timestamp), but when I execute the script for stopping it the start time changes, so for example I started at 9:00, that time will be changed to for example 11:00 when I stop it... very strange, here are my scripts:

 

start:

 

<?php
date_default_timezone_set('Europe/Brussels');
$timestamp = date('Y-m-d H:i:s');
$conn = mysql_connect('server' 'user', 'pw') or die(mysql_error());
mysql_select_db('hystudi_timesheets', $conn) or die(mysql_error());
mysql_query("INSERT INTO timesheet (t_start_timestamp) VALUES ('$timestamp')", $conn) or die(mysql_error());
?>

 

stop:

 

<?php
date_default_timezone_set('Europe/Brussels');
$timestamp = date('Y-m-d H:i:s');
$conn = mysql_connect('server', 'user', 'pw') or die(mysql_error());
mysql_select_db('hystudi_timesheets', $conn) or die(mysql_error());
$records = mysql_query("SELECT id, t_start_timestamp FROM timesheet ORDER BY id DESC LIMIT 1");
while ($row = mysql_fetch_array($records)) {
        $id = $row['id'];
        $start_timestamp = date("U", strtotime($row['t_start_timestamp']));
}
$duration = (date("U", strtotime($timestamp)) - $start_timestamp) / 3600; // duration
mysql_query("UPDATE timesheet SET t_end_timestamp = '$timestamp', t_calculated_time = '".round($duration,2)."' WHERE id = $id", $conn) or die(mysql_error());
?>

 

Anyone sees what I'm doing wrong here? Or could it be my server? It's a shared host

Link to comment
Share on other sites

I'm new to PHP but done some C++, but as much as I see they are very similar. when you assign value to variable, it's stored in memory until it's unset "PHP". you using the same variable $timestamp in stop script as in start, statement: $timestamp = date('Y-m-d H:i:s'); in stop script gives $timestamp date-time value of time stop script is executed, hence reassigning new date-time value. In stop script, replace $timestamp variable with something different eg: $stop_timestamp or anything you fancy, so stop script should look like this:

 

 

<?php

date_default_timezone_set('Europe/Brussels');

$stop_timestamp = date('Y-m-d H:i:s');

$conn = mysql_connect('server', 'user', 'pw') or die(mysql_error());

mysql_select_db('hystudi_timesheets', $conn) or die(mysql_error());

$records = mysql_query("SELECT id, t_start_timestamp FROM timesheet ORDER BY id DESC LIMIT 1");

while ($row = mysql_fetch_array($records)) {

        $id = $row['id'];

        $start_timestamp = date("U", strtotime($row['t_start_timestamp']));

}

$duration = (date("U", strtotime($stop_timestamp)) - $start_timestamp) / 3600; // duration

mysql_query("UPDATE timesheet SET t_end_timestamp = '$stop_timestamp', t_calculated_time = '".round($duration,2)."' WHERE id = $id", $conn) or die(mysql_error());

?>

 

As I've mentioned I'm new to PHP, so hopefully this solution helps. If not, hopefully somebody else will come up with it.

 

Link to comment
Share on other sites

Thanks lenstanbera,

 

It didn't work the way you say, but what I've done is when I get the record I take the t_start_timestamp value and store it in a seperate variable, then I update both t_start_timestamp AND t_end_timestamp.

 

Seems that the problem was my hosting server, since this one is in the US and I'm in Belgium the server updated the t_start_timestamp to the local acutal time. After checking the parameters of my table I saw that I've set the t_start_timestamp to 'CURRENT TIME' or something like that ... changed this and problem solved :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.