Link to home
Start Free TrialLog in
Avatar of Eddie Shipman
Eddie ShipmanFlag for United States of America

asked on

PHP to C# conversion

I have this PHP code to compute a ticket# based on someone's ID and the current time.
I need a C# version of this. Can anyone help out?

$Cust_ID = "1158341";
$reftime = mktime(0,0,0,12,31,1973);
$preticket = floor((time()-$reftime)/(3600*24*7))*$Cust_ID;
$ticket = 'f' . dechex($Cust_ID) . md5($preticket);
echo $ticket;

Open in new window

Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

ASKER

For reference, this produces the same output...

static void Main(string[] args)
{
    string Cust_ID = "1158341";
    int reftime = ConvertToUnixTimestamp(new DateTime(1973, 12, 31));
    int now = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
    string preticket = (Math.Floor(Convert.ToDouble(now - reftime) / Convert.ToDouble(3600 * 24 * 7)) * Int32.Parse(Cust_ID)).ToString();
    string hexval = Int32.Parse(Cust_ID).ToString("X");
    string ticket = string.Concat("f", hexval, MD5Hash(preticket)).ToLower();
    Console.WriteLine(ticket);
    Console.ReadKey();

}
static int ConvertToUnixTimestamp(DateTime date)
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    TimeSpan diff = date - origin;
    return (int)diff.TotalSeconds;
}
public static string MD5Hash(string text)
{
    System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
    return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(md5.ComputeHash(ASCIIEncoding.Default.GetBytes(text))), "-", "");
}

Open in new window

SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Looks OK, kaufmed but I get these in VS2010:
Error	1	'int' does not contain a definition for 'TotalSeconds' 
  and no extension method 'TotalSeconds' accepting a first argument of type 'int' could be found 
  (are you missing a using directive or an assembly reference?)
Error	2	The best overloaded method match for 'System.Convert.ToString(object, System.IFormatProvider)' 
  has some invalid arguments	
Error	3	Argument 2: cannot convert from 'int' to 'System.IFormatProvider'

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Self Answered