php date + time conversion - php

i would to know how can i parse date and time which we choose from date time picker into standard date('YmdHis') type of results?
$datetime = date('YmdHis);
eg date picker return result 11/13/2012 and time picker result is 2.30 AM, now i need to know how to joining them together and convert it to 20121113023000 format?

echo date("YmdHis",strtotime('11/13/2012 2.30 AM'));
or
$data = $_POST['date_picker']." ".$_POST['time_picker'];
echo date("YmdHis",strtotime($data));
this will output that in required format
outputs
20121113023000

This should work
$datatime = date('YmdHis',strtotime($_POST['datepickerfield']));

echo date("Y-m-d H:i:s",strtotime("11/13/2012 2.30 AM"));
outputs
2012-11-13 02:30:00

Related

Convert SQLite to MySQL datetime

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

How to convert iso date time with milliseconds to date time format in php so that milliseconds don't go away?

For eg I have ISO date time as : 2020-03-03T11:07:41.1708478Z
Converting using strtotime function
$dateTime = date("Y-m-d H:i:s.u",strtotime('2020-03-03T11:07:41.1708478Z'));
Result : 2020-03-03 11:07:41.000000
In above result you can see milliseconds are gone.
Use DateTime because strtotime and date only uses full seconds.
$date = new DateTime('2020-03-03T11:07:41.1708478Z');
echo $date->format("Y-m-d H:i:s.u"); // 2020-03-03 11:07:41.170847
https://3v4l.org/DXVj8
But since this I assume this input format is rater fixed and wont change I could recommend you to use a simple str_replace.
echo str_replace(["T","Z"], [" ",""], '2020-03-03T11:07:41.1708478Z'); // 2020-03-03 11:07:41.170847

Convert excel decimal number into date

I have a number on which after applying some formula I got 43917.60417. Excel convert this number into date and datetime format as 3/27/2020 14:30:00. How can I convert this number into date or datetime using mysql or php. I searched a lot but didn't got any solution.
I tried
SELECT FROM_UNIXTIME(REPLACE('13577.77916', '.', ''));
-- this results in 2013-01-10 06:01:56
SELECT FROM_UNIXTIME(REPLACE('43917.60417', '.', ''));
-- this results in null
Here you go in PHP
<?php
$unix = (43917.60417 - 25569) * 86400; //convert to unix first
echo gmdate("d-m-Y H:i:s", $unix); //return to your desired date format
?>
AND IN MySQL, This should work for both positive and negative epoch
SELECT DATE_ADD(FROM_UNIXTIME(0), interval (13577.77916-25569)*86400 second);
And to specify date format in MySQL, you can use DATE_FORMAT(
SELECT DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), interval (13577.77916-25569)*86400 second),'%Y-%m-%d') as my_new_date;
The DateTime extension dt knows a special method createFromMsTimestamp for the MS Excel timestamp.
$excelTimestamp = 43917.60417;
$date = dt::createFromMsTimestamp($excelTimestamp);
echo $date->format('Y-m-d H:i:s');
//2020-03-27 14:30:00

yii2 pick time from datetime

I want to know how to pick the time format from datetime in yii2.
So if there is code like this
$model->date = date('Y-m-d H:i:s', strtotime('+5 hours'));
and the result would be 2018-05-08 23:36:21
how can I extract the time only? so the result is only 23:36:21
I already tried using code below
date("H:i:s", strtotime('-30 minutes'));
but I only got like 00:30:00
Is there something wrong with the code?
Any help would be appreciated :)
if you have the string 2018-05-08 23:36:21 and want to extract time, you can follow the following methods
Using php:date() function
Remove -30m from the time when you format the date
echo date('H:i:s',strtotime('2018-05-08 23:36:21 -30 minutes'));
Using DateTime Object
The method sub() can subtract from the time and output the remaining time using $dateTimeObj->format().
$date1=new \DateTime('2018-05-08 23:36:21');
$date1->sub(new \DateInterval('PT30M'));
echo $date1->format('H:i:s');

how do I add a date with a number?

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.

Categories