Hello, I am trying to work a script that will randomize a datestamp. I don't program that well, so I found this out in google-land: <? ////////////////////////////////////////////////////////// // Return a random timestamp between two dates (inclusive) // Example: Tue, 08 Nov 2004 06:47:10 GMT // // time - Starting time string // Valid Examples: // 10 September 2001 // next Thursday // last Monday // now // // time2 - Ending time string function randomTimestamp($time = "" , $time2 = "") { if(!$time) $time = strtotime("10 September 2000"); if(!$time2) $time2 = strtotime("24 November 2005"); $timestamp = date(" D, d M Y", rand( settype($time , int) , settype($time2 , int) )); //Must be called once before becoming random, ??? $timestamp = date(" D, d M Y", rand($time , $time2))." ";//Now it's random $h = rand(1,23); if(strlen($h) == 1 ) $h = "0$h"; $t = $h.":"; $d = rand(1,29); if(strlen($d) == 1 ) $d = "0$d"; $t .= $d.":"; $s = rand(0,59); if(strlen($s) == 1 ) $s = "0$s"; $t .= $s; $timestamp .= $t." GMT"; return $timestamp; } ?> PHP: My issue with it is that i can't get it to return anything. I've tried changing "return $timestamp" to "echo $timestamp" -- I've used this script as an include for another script and tried echoing it out that way, but no avail. Like I said, I'm really not much of a programmer, but I can edit things if I get pointed in the right direction. Can someone take a look at this and let me know what I'm missing? I'm sure it's something obvious and stupid on my part. Thanks, Marceia
If a function says 'return $something' it'd mean you'd have to assign the function itself to a variable, like so: $datestamp = randomTimestamp('date1','date2'); PHP: and $datestamp should hold the value. Also, try changing settype($time , int) PHP: to this settype($time , 'int') PHP: , and again for the other one. It's possible those functions we're erroring which meant it didn't return anything.
Thanks decepti0n - I implemented, but still have no output. my guess is due to how I'm using: $datestamp = randomTimestamp('date1','date2'); PHP: My question here would be twofold: 1. when I return - will I now be returning $datestamp? -- I think yes. 2. also, here you use 'date1', 'date2' - but that is not called anywhere else in the code. should I use that elsewhere in the code to make this line work? thanks!
1. I don't really get it, $datestamp would hold the return value from the function, which in this case is a 'random timestamp in between 2 dates'. Add echo $datestamp; underneath it to see what comes up 2. They were just examples, instead of date1 it'd be something like 10 September 2000 etc. If you want to just test it, try using: $datestamp = randomTimestamp(); PHP: Since it would just use the default values
how annoying that this isn't working. Here's what my amended code looks like: <?php ////////////////////////////////////////////////////////// // Return a random timestamp between two dates (inclusive) // Example: Tue, 08 Nov 2004 06:47:10 GMT // // time - Starting time string // Valid Examples: // 10 September 2001 // next Thursday // last Monday // now // // time2 - Ending time string $datestamp = randomTimestamp(); function randomTimestamp($time = "" , $time2 = "") { if(!$time) $time = strtotime("10 September 2000"); if(!$time2) $time2 = strtotime("24 November 2005"); $timestamp = date(" D, d M Y", rand( settype($time , 'int') , settype($time2 , 'int') )); //Must be called once before becoming random, ??? $timestamp = date(" D, d M Y", rand($time , $time2))." ";//Now it's random $h = rand(1,23); if(strlen($h) == 1 ) $h = "0$h"; $t = $h.":"; $d = rand(1,29); if(strlen($d) == 1 ) $d = "0$d"; $t .= $d.":"; $s = rand(0,59); if(strlen($s) == 1 ) $s = "0$s"; $t .= $s; $timestamp .= $t." GMT"; return $datestamp; } echo $datestamp; ?> PHP: I have tried having: $datestamp = randomTimestamp(); PHP: where it is right now and also immediately before the cho command. am I doing something really stupid, like a typo I'm just not seeing or something? bah!
Yeah, inside the function it should still be return $timestamp;. There's a section on variable scope on php.net, inside the function we're working on $timestamp, but when its returned it's assigned to $datestamp (totally different variable)
When i tried it before the echo, it was immediately before, so after the close of the function and the return. what's up with this script, you think? I suppose i could try it the hard way and make an array of months, days, hours, etc and then randomize output. Afterall, it IS a random timestamp.
<?php ////////////////////////////////////////////////////////// // Return a random timestamp between two dates (inclusive) // Example: Tue, 08 Nov 2004 06:47:10 GMT // // time - Starting time string // Valid Examples: // 10 September 2001 // next Thursday // last Monday // now // // time2 - Ending time string $datestamp = randomTimestamp(); function randomTimestamp($time = "" , $time2 = "") { if(!$time) $time = strtotime("10 September 2000"); if(!$time2) $time2 = strtotime("24 November 2005"); $timestamp = date(" D, d M Y", rand( settype($time , 'int') , settype($time2 , 'int') )); //Must be called once before becoming random, ??? $timestamp = date(" D, d M Y", rand($time , $time2))." ";//Now it's random $h = rand(1,23); if(strlen($h) == 1 ) $h = "0$h"; $t = $h.":"; $d = rand(1,29); if(strlen($d) == 1 ) $d = "0$d"; $t .= $d.":"; $s = rand(0,59); if(strlen($s) == 1 ) $s = "0$s"; $t .= $s; $timestamp .= $t." GMT"; return $timestamp; } echo $datestamp; ?> PHP: Try that (unless you already did). It doesn't matter where the function is, and it'll only matter if $datestamp = randomTimestamp(); PHP: is anywhere before echo $datestamp; PHP:
Check this out : function randomTimeStamp($time = "", $time2 = "") { if ($time=="") $time = strtotime("10 September 2000"); if ($time2=="") $time = strtotime("24 November 2005"); if (is_string($time)) $time = strtotime($time); if (is_string($time2)) $time2 = strtotime($time2); $randomTimeStamp = rand($time,$time2); return $randomTimeStamp; } $dateStamp = randomTimeStamp(); PHP: I hope it works. The function can get time-stamps and strings (strtotime) .
Decetpi0n: yep, I had tried that. no luck. Morishani: your code works and I now have an output! yea! Now, how do I convert the Unix timestamp back to a normal timestamp and echo that? thanks much to both of you for your help on this. I've looked at the different time variables via google, and I can see how to just echo out a timestamp, but I'm not sure how to grab that random number we just generated and convert IT into a normal timestamp...
Glad you got it working, I wasn't sure if it was the function itself that was just stupid because it seemed a bit complicated. To convert it into something normal you'll have to learn the date() function, and wind up with something like this: echo date('d/M/Y H:i', $datestamp); PHP: Which would be something like 20/01/08 04:56 etc
you know, I wondered the same about the function because some things looked wonky in it. I had taken the 1st step to fix the "if" statement, but would have never figured out the rest of the things that Morishani corrected. I am VERY appreciative of the team effort here. I stuck your default date function in and every refresh gives me a new datestamp - just what i wanted. I'll play around with it to get the exact output I want. Thanks again, all! Marceia