Jump to content

Recommended Posts

I'm using the following as part of a unique ID:

 

$timestamp = date('Ymd.His');

 

However, I'm noticing the timestamp is getting set twice.  First when the page loads and second when the is submitted.  What can I do to only use the timestamp generated at page load?

Hi,

 

I presume you are using a form which self submits (e.g. has a blank action and submits back to the same script), and so the timestamp is set 1) When the form loads 2) When the form is submitted.

The solution to this is to check if the form has been submitted. If it has, don't bother setting the timestamp.

 

For example:

 

1) Add a hidden field to your form which you will check in order to ascertain if the form has been submitted or not:

 

<input type="hidden" name="postback" id="postback" value="1" />

 

2) Then in your script check for the existence of the postback variable and only set the timestamp if the variable does not exist (e.g. the form has not been posted yet).

 

if(!isset($_POST['postback']))
   $timestamp = date('Ymd.His');

 

Please not I am assuming you are using a post form and not a get form.

In my opinion, in most situations, Unix timestamps are better (in the sense of more usability).

 

They are numeric, and are the number of seconds since the Unix epoch (1/1/1970 00:00 GMT).

 

Then, you can format the number into a human readable date later.

 

http://php.net/time

http://php.net/date

 

 

Sorry for the offtopic-ness of this post, but I've noticed that people who use timestamps like you're doing often regret it later.

Thanks.  I've tried the hidden input and the session to no avail.  Still does this:

 

20080709.100331

 

and then:

 

20080709.100335

 

Very odd.  Maybe too many moving parts.  Here's what I have so far.  The sflow function calls cURL to a web site.

 

if (!isset($_SESSION['tstamp'])) {
session_start();
   $_SESSION['tstamp'] = date('Ymd.His');
}

$ordnum = $_POST['ordernumber'];
$serialnum = $_POST['serialnumber'];

if (isset($ordnum)) {

$flowname = "SRP";
$fresult = sflow($flowname);
}

 

Then I have a AJAX reading the file:

 

var fileName = myWan + '\-' + myDate; 

function status()
{
var myAjax = new Ajax.PeriodicalUpdater('status', 'readfile.php?fname=' + fileName, {method: 'get', frequency: 5.0, decay: 1});
}

Replace:

 

if (!isset($_SESSION['tstamp'])) {
session_start();
   $_SESSION['tstamp'] = date('Ymd.His');
}

 

with:

 

session_start();
if (!isset($_SESSION['tstamp'])) {
   $_SESSION['tstamp'] = date('Ymd.His');
}

 

You need to start the session before trying to see if the timestamp is set in it.

 

Robin

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.