-1

I have a time problem. I'm making a karaoke website were I use php for certain operations. The site is made with Drupal. My problem is that I'm using date() to make a file name of the song that is recorded. But this file name is used in two places on my page. The first place makes the file name as soon as the page loads and "echo" it to be used by an applet. The applet is to upload the file at the end when the user leaves the page after recording the song.

The second place will use the file name when a button is pressed. This code will write the file name into a node (My Songs) to be accessed later on. So both file names have to be the same. The problem is that there is a discrepancy between the times both codes use the constant. So the name of the file written in the node My Songs is not the same as the file name that was uploaded. Because there is 4-5 minutes difference when both codes use the file name. I suppose the function: date('dmhi') is stored in the Constant and not the string (result) of the function. I tried using a variable and initializing it first with a string so it would remain a string, but it didn't work. I'm more of a C++ and VB coder, so I don't get how the variables and constants work in php. I searched the web for days and I'm not finding any answers. I hope you people can help me. Here is part of my code:

First occurance:

<?php

Global $user;
  profile_load_profile($user);
$name = $user->name; 
define("FILE", $title . $name . date('dmhi') . ".spx");


echo "<param name=\"refreshURL\" value=\"http://english.karavid.com/node/2?sampleName=" . FILE . "&movie=" . $movie . "&title=" . $title . "&name=" . $name . "\">\n";

echo "<param name=\"uploadFileName\" value=\"" . FILE . "\">\n";


?>

Second occurance when the button is pressed:

$node1->body = substr($node1->body, 0, -8) . "<tr>
 <th scope=\"col\"><a href=\"http://english.karavid.com/node/2?sampleName=" . FILE . "&movie=" . $movie . "&title=" . $title . "&name=" . $name . "\">" . $title . "</a></th>
<th scope=\"col\">" . date('j M Y') . "</th>
<th scope=\"col\"></th>
</tr> </table>";

node_save($node1);

Thanks in advance for any clue you can give me.

Nathalie

EDIT:

I will try to explain better. I'm generating a page that will build some code inside an applet (a recorder for the voice). Inside that applet, I use php to make part of the applet code. That part is to make the applet upload the recording done by the user. Now, I need a file name that will be different for every user. And if the user records the same song many times during the day, the file name has to be different, so I'm using the date and time inside the file name. With the function date();

But that file name is generated on loading. So the file name will include the time upon loading the page. Later on when the user REALLY DO save the song, time has past but the file name that is uploaded contains the time upon loading not the time of saving. This happens because the file name is "echo"(ed) inside the applet at the time of loading the page. There is no other way I can work it around.

But, I would like to preserve that time so that I can use it again. The thing is when I try to use the file name again, the time is no longer the same. A few minutes has passed. So the file name is not the same. And when the file name is written on another page(node or file) after the recording upon saving , it's not the same as the previous one.

I know it's a bit complicated. But basically, I need to generate a file name with the time included inside it. And use the same file name on two occasions that will be separated with a time frame. But the file name should not change.

2 Answers2

0

I'm not really sure what you're asking.

You could create a function to return the date...

public function myDate(){
    return date('dmhi');
}

And call

echo $this->myDate();

Or if you wanted to set the date on the first call and have subsequent calls retrieve the same date

private $_myDate;
public function myDate(){
    if($this->_myDate == null)
        $this->_myDate = date('dmhi');
    return $this->_myDate;
}

Edit

Try it without $this?

$_myDate = null;
function myDate(){
    if($_myDate == null)
        $_myDate = date('dmhi');
    return $_myDate;
}

$date = myDate();

Edit 2

Why not just do this?

$myDate = date('dmhi');
$file = $title . $name . $myDate . ".spx";

Then

$node1->body = substr($node1->body, 0, -8) . "<tr>
 <th scope=\"col\"><a href=\"http://english.karavid.com/node/2?sampleName=" . $file . "&movie=" . $movie . "&title=" . $title . "&name=" . $name . "\">" . $title . "</a></th>
<th scope=\"col\">" . $myDate . "</th>
<th scope=\"col\"></th>
</tr> </table>";
ameagher
  • 328
  • 2
  • 12
  • Thank you for your answer. I'm sorry if it wasn't clear. English is not my mother tongue. What I want to do is what you said, I need to set the date (and time) on the first call (when loading the page) and have the same date(time) on a subsequent call (at the end when the "save" button is pushed). But the time has a few minutes discrepancy when it's called the second time. I used your code (the second one), but it doesn't work. I call the function like this: $date = $this->myDate(); but it doesn't compile. – Nathalie Audet Oct 05 '13 at 15:45
  • I think we're confusing the issue. You're basically generating a page that will provide a button to access a previously defined file, right? – ameagher Oct 05 '13 at 16:03
  • thank you again. I tried both solutions, they both compiled well, but didn't solve my issue. I appreciate your help and I edited my question to make it cleared. – Nathalie Audet Oct 05 '13 at 16:52
0

So after trying many things, I found a way to work around it. I post it here in case someone needs to do this in the future.

On my page I have a form that I use for the "save" button. I simply added this at the end of the form:

echo "<input type=\"hidden\" value=\"" . FILE . "\" name=\"filename\" id=\"filename\">
</form>";

So it prints the file name I need when the page is loading and it remains hidden. So when the "save" button is pushed later on, the final code runs and I just recuperate the constant that was passed into this hidden field, like this:

$file = $_POST["filename"];

It's not a perfect solution, but it works. If anyone has a better one, please let me know.