0

I need to create a way for a client to have a new PIN number every week, but they are using a shared hosting and it is not letting create/edit files, nor I want to use their MySQL for storing this kind of thing.

I think there should be a way to generate a semi-unique number every week and then CRON job could send a new number to the client every week.

Any ideas how to do that conveniently?

Dovydas Navickas
  • 3,533
  • 1
  • 32
  • 48
  • 1
    `$thisWeek = date('YW'); mt_srand($thisWeek); $PIN = mt_rand(10000000,99999999); echo $PIN;` – Mark Baker Sep 18 '14 at 16:53
  • I need a constant value for that week, but that it would change every week, so random is not an option as it would give me a random different value every time. – Dovydas Navickas Sep 18 '14 at 16:58
  • Have you actually tried seeding with a known value immediately before asking for a random number..... bet I can predict the exact value you'll get if you run the code exactly as I've just posted it.... I can't predict the value it will give next week without running it, but I can tell you that it will be constant throughout that week, then change again the following week – Mark Baker Sep 18 '14 at 17:06
  • Well, Giedrius' answer is really what I've been looking for. When it is run in server, I can get a different value for every week, but predicting it is not as easy also. – Dovydas Navickas Sep 18 '14 at 17:08
  • P.S. the value for this week is `80239132` – Mark Baker Sep 18 '14 at 17:10
  • Ok.. I did not see the "s" in mt_srand :) Your code does just that – Dovydas Navickas Sep 18 '14 at 17:15

1 Answers1

2

Date.prototype.getWeek = function() {
    var onejan = new Date(this.getFullYear(), 0, 1);
    return Math.ceil((((this - onejan) / 86400000) + onejan.getDay() + 1) / 7);
}

function GeneratePIN(year, month, day){
  var date = new Date();
  date.setYear(year);
  date.setMonth(month);
  date.setDate(day);
  
  var weekNumber = date.getWeek();
  month = date.getMonth()+1;
  return Math.round(weekNumber * month * Math.PI * 500000)
                .toString()
                .substr(0,4)
                .split("")
                .reverse()
                .join("");
}


angular.module('MyApp', [])
.controller('MyCtrl', function($scope){
  items = [];
  for(var i = 0; i < 12; i++){
      items.push({date : new Date(2014,i,1), pin : GeneratePIN(2014, i+1, 1)});
    }
    $scope.items = items;
});
<!DOCTYPE html>
<html ng-app="MyApp">

  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  </head>

  <body>
    <h1>Sample</h1>
    <div ng-controller="MyCtrl">
      <div ng-repeat="item in items">
        {{ item.date | date:'yyyy-MM-dd HH:mm:ss' }} => {{item.pin}}
      </div>
    </div>
  </body>

</html>

You can use a formula, that calculates pin using week number.

Someting like this:

weekNumber * month * Math.PI * 500000

Add your "secret" constants and it will give you not easily predictable numbers, but they will still differ from week to week.

Dovydas Navickas
  • 3,533
  • 1
  • 32
  • 48
Giedrius
  • 94
  • 4