Php 如何每x个月生成一个随机数?
从数字9开始,使用php,我希望能够从那里开始数数,然后以1的增量回显下一个数字。 因此,数字9,然后在1个月后,数字将变为10,然后是另一个月11,然后是12,以此类推,没有最大数字/停止点 我怎样才能做到这一点?到目前为止,我有以下代码Php 如何每x个月生成一个随机数?,php,Php,从数字9开始,使用php,我希望能够从那里开始数数,然后以1的增量回显下一个数字。 因此,数字9,然后在1个月后,数字将变为10,然后是另一个月11,然后是12,以此类推,没有最大数字/停止点 我怎样才能做到这一点?到目前为止,我有以下代码 $number = 9; $output = $number + 1; echo $output; 有没有办法将此设置为每月增加一次?根据您的问题,我会说: $number = 9; $output = date('n') + $number; echo
$number = 9;
$output = $number + 1;
echo $output;
有没有办法将此设置为每月增加一次?根据您的问题,我会说:
$number = 9;
$output = date('n') + $number;
echo $output;
但这取决于你想要完成什么。您还可以用模将数字环绕在date()
周围
然而,这并不是随机的。如果您希望按照主题建议每月创建一个随机数,请使用该月作为随机种子
srand(date('n'));
$number = rand();
根据你的问题,我想说:
$number = 9;
$output = date('n') + $number;
echo $output;
但这取决于你想要完成什么。您还可以用模将数字环绕在date()
周围
然而,这并不是随机的。如果您希望按照主题建议每月创建一个随机数,请使用该月作为随机种子
srand(date('n'));
$number = rand();
根据你的问题,我想说:
$number = 9;
$output = date('n') + $number;
echo $output;
但这取决于你想要完成什么。您还可以用模将数字环绕在date()
周围
然而,这并不是随机的。如果您希望按照主题建议每月创建一个随机数,请使用该月作为随机种子
srand(date('n'));
$number = rand();
根据你的问题,我想说:
$number = 9;
$output = date('n') + $number;
echo $output;
但这取决于你想要完成什么。您还可以用模将数字环绕在date()
周围
然而,这并不是随机的。如果您希望按照主题建议每月创建一个随机数,请使用该月作为随机种子
srand(date('n'));
$number = rand();
一个非常低效的方法是
<?php
function increm($duration){
while ($i<$duration) {
$i++;
}
return true;
}
$number = 9;
$start = time();
$i = 0;
while (1){
increm(3600*24*30);
$i++;
// Do your code
}
?>
<?php
$number = 9;
if(!file_exists('date.txt')){
$date=date('n');
file_put_contents( (string)time());
$date = 0;
}
else{
$date= file_get_contents('date.txt');
$date= date()-(int)$date;
$date= floor($date/(24*3600*30));
}
// do whatever you may
?>
此脚本必须连续运行数月
更好的办法是
<?php
function increm($duration){
while ($i<$duration) {
$i++;
}
return true;
}
$number = 9;
$start = time();
$i = 0;
while (1){
increm(3600*24*30);
$i++;
// Do your code
}
?>
<?php
$number = 9;
if(!file_exists('date.txt')){
$date=date('n');
file_put_contents( (string)time());
$date = 0;
}
else{
$date= file_get_contents('date.txt');
$date= date()-(int)$date;
$date= floor($date/(24*3600*30));
}
// do whatever you may
?>
但是这个脚本会在每次调用时增加它,因为第一个打开日期将被存储。将永远工作(直到UNIX可以使用时间戳)。一个非常低效的方法是
<?php
function increm($duration){
while ($i<$duration) {
$i++;
}
return true;
}
$number = 9;
$start = time();
$i = 0;
while (1){
increm(3600*24*30);
$i++;
// Do your code
}
?>
<?php
$number = 9;
if(!file_exists('date.txt')){
$date=date('n');
file_put_contents( (string)time());
$date = 0;
}
else{
$date= file_get_contents('date.txt');
$date= date()-(int)$date;
$date= floor($date/(24*3600*30));
}
// do whatever you may
?>
此脚本必须连续运行数月
更好的办法是
<?php
function increm($duration){
while ($i<$duration) {
$i++;
}
return true;
}
$number = 9;
$start = time();
$i = 0;
while (1){
increm(3600*24*30);
$i++;
// Do your code
}
?>
<?php
$number = 9;
if(!file_exists('date.txt')){
$date=date('n');
file_put_contents( (string)time());
$date = 0;
}
else{
$date= file_get_contents('date.txt');
$date= date()-(int)$date;
$date= floor($date/(24*3600*30));
}
// do whatever you may
?>
但是这个脚本会在每次调用时增加它,因为第一个打开日期将被存储。将永远工作(直到UNIX可以使用时间戳)。一个非常低效的方法是
<?php
function increm($duration){
while ($i<$duration) {
$i++;
}
return true;
}
$number = 9;
$start = time();
$i = 0;
while (1){
increm(3600*24*30);
$i++;
// Do your code
}
?>
<?php
$number = 9;
if(!file_exists('date.txt')){
$date=date('n');
file_put_contents( (string)time());
$date = 0;
}
else{
$date= file_get_contents('date.txt');
$date= date()-(int)$date;
$date= floor($date/(24*3600*30));
}
// do whatever you may
?>
此脚本必须连续运行数月
更好的办法是
<?php
function increm($duration){
while ($i<$duration) {
$i++;
}
return true;
}
$number = 9;
$start = time();
$i = 0;
while (1){
increm(3600*24*30);
$i++;
// Do your code
}
?>
<?php
$number = 9;
if(!file_exists('date.txt')){
$date=date('n');
file_put_contents( (string)time());
$date = 0;
}
else{
$date= file_get_contents('date.txt');
$date= date()-(int)$date;
$date= floor($date/(24*3600*30));
}
// do whatever you may
?>
但是这个脚本会在每次调用时增加它,因为第一个打开日期将被存储。将永远工作(直到UNIX可以使用时间戳)。一个非常低效的方法是
<?php
function increm($duration){
while ($i<$duration) {
$i++;
}
return true;
}
$number = 9;
$start = time();
$i = 0;
while (1){
increm(3600*24*30);
$i++;
// Do your code
}
?>
<?php
$number = 9;
if(!file_exists('date.txt')){
$date=date('n');
file_put_contents( (string)time());
$date = 0;
}
else{
$date= file_get_contents('date.txt');
$date= date()-(int)$date;
$date= floor($date/(24*3600*30));
}
// do whatever you may
?>
此脚本必须连续运行数月
更好的办法是
<?php
function increm($duration){
while ($i<$duration) {
$i++;
}
return true;
}
$number = 9;
$start = time();
$i = 0;
while (1){
increm(3600*24*30);
$i++;
// Do your code
}
?>
<?php
$number = 9;
if(!file_exists('date.txt')){
$date=date('n');
file_put_contents( (string)time());
$date = 0;
}
else{
$date= file_get_contents('date.txt');
$date= date()-(int)$date;
$date= floor($date/(24*3600*30));
}
// do whatever you may
?>
但是这个脚本会在每次调用时增加它,因为第一个打开日期将被存储。将永远工作(直到UNIX可以使用时间戳)。您可以使用PHP-function实现这一点。这是一个这样做的例子,如果你不依赖于一个月的哪一天,但添加一天的功能是可能的,应该很容易
$startNumber = 9;
$startYear = 2015;
$startMonth = 9;
$currentYear = intval( date( "Y" ) );
$currentMonth = intval( date( "n" ) );
$monthsToAdd = ( ( $currentYear - $startYear ) * 12 )
+ ( $currentMonth - $startMonth );
echo $startNumber + $monthsToAdd;
您可以使用PHP-函数实现这一点。这是一个这样做的例子,如果你不依赖于一个月的哪一天,但添加一天的功能是可能的,应该很容易
$startNumber = 9;
$startYear = 2015;
$startMonth = 9;
$currentYear = intval( date( "Y" ) );
$currentMonth = intval( date( "n" ) );
$monthsToAdd = ( ( $currentYear - $startYear ) * 12 )
+ ( $currentMonth - $startMonth );
echo $startNumber + $monthsToAdd;
您可以使用PHP-函数实现这一点。这是一个这样做的例子,如果你不依赖于一个月的哪一天,但添加一天的功能是可能的,应该很容易
$startNumber = 9;
$startYear = 2015;
$startMonth = 9;
$currentYear = intval( date( "Y" ) );
$currentMonth = intval( date( "n" ) );
$monthsToAdd = ( ( $currentYear - $startYear ) * 12 )
+ ( $currentMonth - $startMonth );
echo $startNumber + $monthsToAdd;
您可以使用PHP-函数实现这一点。这是一个这样做的例子,如果你不依赖于一个月的哪一天,但添加一天的功能是可能的,应该很容易
$startNumber = 9;
$startYear = 2015;
$startMonth = 9;
$currentYear = intval( date( "Y" ) );
$currentMonth = intval( date( "n" ) );
$monthsToAdd = ( ( $currentYear - $startYear ) * 12 )
+ ( $currentMonth - $startMonth );
echo $startNumber + $monthsToAdd;
为此,必须将数字存储在数据库中,与当前unix时间戳进行比较,并在到达新月份时进行更新 2个数据库列:count\u month int(10)和next\u month int(10)其中next\u month将包含下个月第一天的unix时间戳。您可以使用cronjobs或在生产环境中运行它
<?php
$now = strtotime("now");
$next_month = strtotime("first day of next month");
if ($query = $dbconnect->prepare("SELECT next_month FROM table1")) {
$query->execute();
$query->bind_result($compare_time);
$query->store_result();
$row_count = $query->num_rows;
if ($row_count > 0) {
while ($query->fetch()) {
if ($compare_time < $now) { // you reached the 1th of the next month time to update
if ($query2 = $dbconnect->prepare("UPDATE table1 SET count_month=count_month +1, next_month=?")) {
$query2->bind_param('i', $next_month);
$query2->execute();
$query2->close();
}
}
}
}
$query->free_result();
$query->close();
}
?>
为此,您必须将数字存储在数据库中,与当前unix时间戳进行比较,并在到达新月份时进行更新 2个数据库列:count\u month int(10)和next\u month int(10)其中next\u month将包含下个月第一天的unix时间戳。您可以使用cronjobs或在生产环境中运行它
<?php
$now = strtotime("now");
$next_month = strtotime("first day of next month");
if ($query = $dbconnect->prepare("SELECT next_month FROM table1")) {
$query->execute();
$query->bind_result($compare_time);
$query->store_result();
$row_count = $query->num_rows;
if ($row_count > 0) {
while ($query->fetch()) {
if ($compare_time < $now) { // you reached the 1th of the next month time to update
if ($query2 = $dbconnect->prepare("UPDATE table1 SET count_month=count_month +1, next_month=?")) {
$query2->bind_param('i', $next_month);
$query2->execute();
$query2->close();
}
}
}
}
$query->free_result();
$query->close();
}
?>
为此,您必须将数字存储在数据库中,与当前unix时间戳进行比较,并在到达新月份时进行更新 2个数据库列:count\u month int(10)和next\u month int(10)其中next\u month将包含下个月第一天的unix时间戳。您可以使用cronjobs或在生产环境中运行它
<?php
$now = strtotime("now");
$next_month = strtotime("first day of next month");
if ($query = $dbconnect->prepare("SELECT next_month FROM table1")) {
$query->execute();
$query->bind_result($compare_time);
$query->store_result();
$row_count = $query->num_rows;
if ($row_count > 0) {
while ($query->fetch()) {
if ($compare_time < $now) { // you reached the 1th of the next month time to update
if ($query2 = $dbconnect->prepare("UPDATE table1 SET count_month=count_month +1, next_month=?")) {
$query2->bind_param('i', $next_month);
$query2->execute();
$query2->close();
}
}
}
}
$query->free_result();
$query->close();
}
?>
为此,您必须将数字存储在数据库中,与当前unix时间戳进行比较,并在到达新月份时进行更新 2个数据库列:count\u month int(10)和next\u month int(10)其中next\u month将包含下个月第一天的unix时间戳。您可以使用cronjobs或在生产环境中运行它
<?php
$now = strtotime("now");
$next_month = strtotime("first day of next month");
if ($query = $dbconnect->prepare("SELECT next_month FROM table1")) {
$query->execute();
$query->bind_result($compare_time);
$query->store_result();
$row_count = $query->num_rows;
if ($row_count > 0) {
while ($query->fetch()) {
if ($compare_time < $now) { // you reached the 1th of the next month time to update
if ($query2 = $dbconnect->prepare("UPDATE table1 SET count_month=count_month +1, next_month=?")) {
$query2->bind_param('i', $next_month);
$query2->execute();
$query2->close();
}
}
}
}
$query->free_result();
$query->close();
}
?>
使用date()
功能使用date()
功能使用date()
功能使用date()
功能因此底部选项将是最佳解决方案?date.txt是什么?这个数字会重置吗?不会。没有“最佳”或“最差”的解决方案,它们只是有效或低效的。在底部,数字永远不会重置。由于起始月份保存在date.txt文件中(可以是任何其他文件),因此开销会减少。不过,如果你考虑生成随机数@Yorick_de_Wid的解决方案会更好,那么底部选项将是最好的解决方案?date.txt是什么?W