Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert date into integer number in php?

Tags:

date

php

mysql

I have an mysql database with a date column and i am using datatype datetime.

But now i want change my datatype from datetime to long integer

I would like to know how to convert date to any integer value.

Let say i have a date

i.e 2012-03-27 18:47:00

so I was wondering if it's possible to convert into any integer number like 131221154

like image 402
शु-Bham Avatar asked Feb 15 '14 11:02

शु-Bham


People also ask

How can I change the date format in PHP?

Change YYYY-MM-DD to DD-MM-YYYY In the below example, we have date 2019-09-15 in YYYY-MM-DD format, and we will convert this to 15-09-2019 in DD-MM-YYYY format. $orgDate = "2019-09-15"; $newDate = date("d-m-Y", strtotime($orgDate)); echo "New date format is: ".

What is the use of Strtotime function in PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


1 Answers

Use strtotime function of PHP.

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

echo strtotime('2012-03-27 18:47:00'); //--> which results to 1332866820

And to make it back again, just use the date function of PHP:

$long = strtotime('2012-03-27 18:47:00'); //--> which results to 1332866820
echo date('Y-m-d H:i:s', $long);
like image 162
Vainglory07 Avatar answered Sep 24 '22 01:09

Vainglory07