C # time-to-integer (timestamp), mimicking some functions of PHP strtotime functions

Source: Internet
Author: User

Today, we need to import a news system data based on MS SQL database into Phpcms v9, the source system news date format is "2014-01-15 10:45:49",
And Phpcms is used in the integer timestamp, in PHP is very simple, with strtotime () can be;
In C #, you need to write your own functions, in the following steps:

Step 1: Calculate the base time for the timestamp in the PHPCMS first.
1             TimeSpan ts = new TimeSpan (0,0,0,1389753949); 2             DateTime now = Convert.todatetime ("2014-01-15 10:45:49"); 3             DateTime basetime = now-ts;4             Response.Write (Basetime.tostring ()); 5             Response.Write ("<br/>");

displayed on the page was 1970-1-1 8:00:00, the base time was obtained

Step 2: The conversion time is an integer timestamp.
1  //benchmark for "1970-1-1 8:00:00" time to integer 2             basetime = Convert.todatetime ("1970-1-1 8:00:00"); 3             ts = DateTime.Now- Basetime;4             Long intervel = (long) ts. Totalseconds;5             Response.Write ("Current time converted to:" + intervel. ToString ());
The resulting integer is from 1970-1-1 8:00:00 to the current number of seconds, namely Phpcms v9 in V9_news table inputtime column, updatetime column value source

Appendix: Date Conversion to timestamp

PHP provides functions that can easily convert various forms of a date to a timestamp, which is mainly:

    • Strtotime (): Resolves the datetime description of any English text to a timestamp.
    • Mktime (): Gets the timestamp from the date.
Strtotime ()

The Strtotime () function is used to convert the date represented by the English text string to a timestamp, an inverse of date (), which returns the timestamp successfully, otherwise FALSE.

Grammar:

int Strtotime (string time [, int now])

The parameter time is the parsed string and is the date represented by the GNU date input format.

Example:

<?phpecho strtotime ("2009-10-21 16:00:10");//Output 1256112010echo Strtotime ("September 2008");//Output 1220976000echo Strtotime ("+1 Day"), "<br/>";//Output the timestamp of tomorrow at this time?>

C # time-to-integer (timestamp), mimicking some functions of PHP strtotime functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.