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
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);
check this
<?php
$timestamp = strtotime('1st January 2004'); //1072915200
// this prints the year in a two digit format
// however, as this would start with a "0", it
// only prints "4"
echo idate('y', $timestamp);
?>
Related
I have SQLite DB one table contains datetime field
with datatype "timestamp" REAL value is 18696.0
attach image for table structure
So, I want this 18696.0 value to be converted into MySQL Y-m-d format and result should be 2021-03-10
I have didn't found any solution online. any help would be appreciated.
SQLite timestamp converted into MySQL timestamp.
EDIT: Thankyou for updating your question with the correct number and what date it should represent.
You can achieve what you need with a function that adds the days onto the Unix Epoch date:
function realDateToYmd($real, $outputFormat='Y-m-d')
{
$date = new DateTime('1970-01-01');
$date->modify('+' . intval($real) . ' days');
return $date->format($outputFormat);
}
echo realDateToYmd('18696.0');
// returns 2021-03-10
SQLite dates stored in REAL data type stores dates as a Julian Day.
From https://www.sqlite.org/datatype3.html
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
PHP has a jdtogregorian function, in which one comment has a handy function to convert to ISO8601 dates:
function JDtoISO8601($JD) {
if ($JD <= 1721425) $JD += 365;
list($month, $day, $year) = explode('/', jdtogregorian($JD));
return sprintf('%+05d-%02d-%02d', $year, $month, $day);
}
echo JDtoISO8601('17889.0');
// Results in -4664-11-16
The results don't exactly look right, is it definitely 17889.0 in SQLite?
If this float number 18696.0 represents the number of days since 1970-01-01 then the date can also be calculated like this:
$days = 18696.0;
$dt = date_create('#'.((int)($days * 86400)));
$mysqlDate = $dt->format('Y-m-d'); //"2021-03-10"
background information
Or simply with gmdate:
$mySqlDate = gmdate('Y-m-d',$days*86400);
The days are simply converted into seconds to get a valid timestamp for gmdate.
Try this:
<?php
echo date('Y-m-d H:i:s', 17889);
?>
Output:
1970-01-01 04:58:09
I want to get date from VARCHAR column.
(eg: 4/14/2018 12:00:00 AM)
How do I display only date
(eg: 4/14/2018)?
SELECT date(created_at) from self_balance
here created_at(varchar)
this returns NULL value
You can run this query to get your output,
SELECT DATE_FORMAT(STR_TO_DATE(created_at, "%Y-%m-%d"), "%Y-%m-%d") FROM
self_balance
First I am matching date format and converting it to date and then formatting.
You can fetch date like a normal string from the database then you need to use strtotime which parses an English textual DateTime into a Unix timestamp. Then you can use
date function which returns the formatted date string. I have passed a static string. You can pass your string variable which you are fetching from the database
$time = strtotime($date_string_from_database);
<?php
$time = strtotime('4/14/2018 12:00:00 AM');
$newformat = date('m/d/Y',$time);
echo $newformat;
?>
You can see the live demo here
I want to compare the file modification date with the current date.
I tried the following (which is working for the current Time):
$currentDay = date("d");
$currentMonth = date("m");
$currentYear = date("y");
$currentHour = date("h");
$currentMinute = date("i");
Now i tried to get the file modification year from my file:
$subst1 = file("f1/subst_001.htm");
$mod_date=date("y", filemtime($subst1));
echo $mod_date;
But it's giving me for year "70", which is coming from the Year 1970, what did i do wrong?
And no, i already checked if the file says this creation year...
The filetime() function expects string as file path. Here you are trying to pass an array that is returned from file()
Try like this way,
$filename= "f1/subst_001.htm";
if (file_exists($filename)) {
$mod_date = date("y", filemtime($filename));
echo $mod_date;
}
January 1, 1970 is the so called Unix epoch. It's the date where they
started counting the Unix time. If you get this date as a return
value, it usually means that the conversion of your date to the Unix
timestamp returned a (near-) zero result. So the date conversion
doesn't succeed. Most likely because it receives a wrong input.
Courtesy : Oldskool
Use the correct date format parameter.
'y' returns a two digit representation of the year:
date ("y", filemtime($filename)) // 18
'Y' returns the four digit representation of the year:
date ("Y", filemtime($filename)) // 2018
Try this:
$mod_date=date("y", filemtime("f1/subst_001.htm"));
echo $mod_date;
filemtime() takes string:pathname as its parameter but file() function reads a file into an array. Hope that helps.
Try using var_dump on the filemtime result - it is probably false (or something which converts to int 0). Since time 0 is January 1, 1970, that would explain why the year is '70'.
If that's the case, it's not finding your file.
I have this date: 1348-10-11 and I have this number: 1438438368. Now I want to know how can I plus them ? like this:
(1348-10-11) + 1438438368 = 1394-5-10 = current date as [Solar]
1348-10-11: the base of Solar date in TIMESTAMP. It is also equal with 1970-01-01 (as Gregorian)
1438438368: Total seconds of 1348-10-11 till now.
anyway for converting {Gregorian date} to {Solar date} I need to add a date with a number, Is it possible ?
Use date() and strtotime() to convert date to timestamp then combine both.
$time = strtotime("1348-10-11") + 1438438368;
echo date("Y-m-d", $time); // Current date
Here is a demo.
I want to display $row->depositdate in dd-mm-yyyy format.
If the date column in database is null the date displayed is : 01-01-1970
echo "<td align=center>".date('d-m-Y', strtotime($row->depositdate))."</td>";
If the date is null in database it should display nothing , otherwise the date in dd-mm-yyyy format should be displayed.
Thanks in advance
Sandeep
NULL is interpreted as 0 by strtotime, since it want to be passed an integer timestamp. A timestamp of 0 means 1-1-1970.
So you'll have to check for yourself if $row->depositdate === NULL, and if so, don't call strtotime at all.
NULL is converted to 0 - the epoch (1-1-1970)
Do this instead
echo "<td align=center>".($row->depositdate ? date('d-m-Y', strtotime($row->depositdate)) : '')."</td>";
You need to check if $row->depositdata is_null previously or check for 0 after strtotime if the value of $row->depositdata is unrecognizable for strtotime.
echo "<td align=center>";
if (!is_null($row->depositdate))
{
$jUnixDate = strtotime($row->depositdate));
if ($jUnixDate > 0)
{
echo date('d-m-Y', $jUnixDate);
}
}
echo "</td>";
strtotime 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.
more about unixtime and Y2K38 problem: http://en.wikipedia.org/wiki/Year_2038_problem
I am able to insert value into DB (SQL Server -- Microsoft) with PHP and value stored is: 2021-03-25 22:45:00
When I trying to fetch the value from DB:
$db = trim($row_table['db_timestamp']);
$timestamp = strtotime('m-d-y',$db);
echo date("m-d-Y H:i:s", $timestamp);
output is : 01-01-1970 05:30:00
Oh!
I know why this happens?
Simply you have not included "depositdate"
in your SELECT query.
Firstly Change SQL query to select all with wild card sign
as shown here
$sql = "SELECT * FROM `yourtable`";