How do I convert 2014-10-10 04:13:24 to 2014-10-10T04:13:24+00:00 in php or mysql
The above mentioned date is for xml format in sitemaps
For reference check Q: How do I compute lastmod date? (Sitemaps.org FAQ)
code which i have tried:
echo "GIVEN DATE ".$timestamp = "2014-10-10 04:13:24";
echo "<br>";
$year = date('Y',strtotime($timestamp)).";
$month = date('m',strtotime($timestamp)).";
$day = date('d',strtotime($timestamp))."";
echo '<br>';
$hour = date('H',strtotime($timestamp))."";
$minutes = date('i',strtotime($timestamp))."";
$seconds = date('s',strtotime($timestamp))."<br>";
$gmktime= gmmktime($hour,$minutes,$seconds,$month,$day,$year)."<br>";
echo "output date".$isodate = date('c', $gmktime);
is the above out put conversion correct?
**OUTPUT**
GIVEN DATE : 2014-10-10 04:13:24
output date : 2014-10-10T06:13:24+02:00
Your output is correct in the light of the Sitemaps.org spec. "2014-10-10T06:13:24+02:00" is the same date/time as "2014-10-10T04:13:24+00:00".
Learn more about the W3C Datetime encoding which is used by Sitemaps.org.
Also, don't solve this with date functions, solve this with string function / operations: You change a single byte inside a string at a fixed position and then you append a string:
$timestamp = "2014-10-10 04:13:24";
$timestamp[10] = "T";
$timestamp .= "+00:00";
echo $timestamp, "\n"; // 2014-10-10T04:13:24+00:00
Or if you like to save some bytes in your file use "Z" instead of "+00:00" to denote the timezone:
$timestamp = "2014-10-10 04:13:24";
$timestamp[10] = "T";
$timestamp .= "Z";
echo $timestamp, "\n"; // 2014-10-10T04:13:24Z
PHP 5:
<?php
// Set the default timezone
date_default_timezone_set('UTC');
// Get Unix timestamp for a date
// Reference: mktime(hour, minute, second, month, day, year)
$time = mktime(04, 13, 24, 10, 10, 2014);
// Alternatively (must be a valid English date format)
$time = strtotime('2014-10-10 04:13:24');
// ISO 8601 date (added in PHP 5)
$isodate = date('c', $time);
echo $isodate; // 2014-10-10T04:13:24+00:00
// RFC 2822 formatted date
$rfcdate = date('r', $time);
echo $rfcdate; // Fri, 10 Oct 2014 04:13:24 +0000
?>
References:
date() / gmdate()
mktime() / gmmktime()
strtotime() : Supported Date and Time Formats
date_default_timezone_set() : List of Supported Timezones
ISO 8601
RFC 2822
In PHP
In PHP you use strptime() to parse the time and turn it into a structured array. Then pass the results of that into the mktime() function to get a UNIX timestamp.
In MySQL
Use UNIX_TIMESTAMP
SELECT UNIX_TIMESTAMP(datetime_column) FROM table;
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 have a string that contains date and time. Format of my string is yyyymmddtime.
For example. 20171125123000209. this is my complete string in which first comes the month, then month and then day after that time. How can i retrieve date from it by converting to readable date format. I tried with php's date function. But the output was not as expected. Please help.
Try this
<?php
$str_date= "20171125123000209";
$exiting_date_format='Ymd';
//first 8 characters from given date string in second parameter below
$date = DateTime::createFromFormat($exiting_date_format, substr($str_date,0,8));
echo $date->format('Y-m-d');//specify desired date format
?>
Output :
2017-11-25
DateTime::createFromFormat - Parses a time string according to a specified format
You can use date() function of php
<?php
$full_date= "20171125123000209";
echo date('dS F h:i:s A', $full_date);
//Output = 31st May 12:30:09 AM
?>
Or,
To get date from timestamp like now,
$timestamp= time(); //Or your timestamp here
$date = date('d-m-Y', $timestamp); //Inside first parameter, give your date format
echo $date; //17-12-2017
Or,
To get anything from string you can also use substr() function of php.
<?php
$full_date= "20171125123000209";
$year = substr($full_date, 0, 4);
$month = substr($full_date, 4, 2);
$date = substr($full_date, 6, 2);
echo 'Year = '.$year.' ';
echo 'Month = '.$month.' ';
echo 'Date = '. $date.' ';
?>
Output:
Year = 2017 Month = 11 Date = 25
Test in jdoodle
About substr() function in php
It's not necessary to manipulate the string at all. PHP's DateTime class supports parsing a string containing miliseconds natively, using the u format modifier:
$str = '20171125123000209';
$date = DateTime::createFromFormat('YmdHisu', $str);
Using your new $date object, you can convert to whatever format you're looking for, e.g.
echo $date->format("F j Y, g:i a");
// November 25 2017, 12:30 pm
See https://eval.in/920636
$your_strtotime_val = ""; //20171125123000209
$convert_to_date = date("m-d-Y h:i:s",$your_strtotime); // [m-d-Y h:i:s] this depending on how you convert date in first time so be careful
I'm required to take a date in the format 'Y-z' which is year-doy (e.g, 2013-146) and convert that into a unix time stamp to be store into a database.
The issue i have is that i input 2013-146 and turn it into a DateTime Object. then when i output this date in unix or 'Y-m-d' format i get 2013-5-27 not 2013-5-26 which is the correct day.
You can verify the DOY on this NASA website and this NOAA website.
Summary:
--I have the date: '2013-146'
--Using DateTime::createFromFormat and echoing using 'Y-m-d' and 'Y-z' i get: 2013-5-27 and 2013-146 respectively.
--This does not agree with the NASA website I listed and is offset by one day can anyone verify that I'm not losing my mind?
Here is the code you can test:
<?php
date_default_timezone_set('America/Chicago');
$year = 2013; //where this outputs a simple year 'CCYY'
$day = 146; //where this provides the day of year
$format = 'Y-z'; //specifying what format i'm creating the datetime with
$date = $year.'-'.$day; //formatting the strings to the above $format
$timezone = new DateTimeZone('America/Chicago'); //specify the timezone
$fileDateStore = DateTime::createFromFormat($format, $date, $timezone);//, $timezone); //create the DateTime object
$fileDateString = date_format($fileDateStore,"Y-m-d"); //format it so strtotime() can read it
$fileDate = strtotime($fileDateString); //finally create the Unix Timestamp for the date.
$newfileDOY = date_format($fileDateStore,"Y-z");
echo 'newfileDOY = '.$newfileDOY.', ';
echo 'date = '.$date.', ';
echo 'fileDateString = '.$fileDateString.', ';
echo 'fileDate = '.$fileDate.PHP_EOL;
?>
The problem is than z format in PHP begins with 0 and not with 1.
Look at: http://www.php.net/manual/en/function.date.php
z: The day of the year (starting from 0)
So I have the date like this in wordpress, I get it from a custom metabox where is stored like this, 23/02/2012, now how can I set wp_locale in WP or something like that, and I need to convert the date to: Monday 23 February 2012, but I need also to set the language, thats why I need that wp_locale because that output will be in Dutch.Thank you
The date format can be converted with the code below as a guide.
date('l j F Y', strtotime(str_replace('/', '-', '23/02/2012')))
The str_replace is necessary because with / PHP assumes m/d/y American date format, not the European d/m/y.
As for doing this in Wordpress with i18n support, you might consider http://codex.wordpress.org/Function_Reference/date_i18n
date_i18n('l j F Y', strtotime(str_replace('/', '-', '23/02/2012')))
I guess there are other ways to do this, but I'd do it as follows:
Upon 'save_post':
$date = explode ('/',$_POST["date_field"]);
$date = $date[1].'/'.$date[0].'/'.$date[2]; // dd/mm/yyyy to mm/dd/yyyy
$s = strtotime($date); /* UNIX TIMESTAMP */
and then store the Unix timestamp in the database.
Upon 'amdin_init' you'll need to do the exact opposite to load the date in the right format in the metabox:
global $post;
$custom = get_post_custom($post->ID);
if ($custom["date_field"][0]) {
$d = date("d/m/Y",$custom["date_field"][0]); // convert unix timestamp
} else {
$d = "";
}
And in your template file, use php functions setlocale and strftime to display the date:
$custom = get_post_custom($post->ID);
$d = $custom["date_field"][0];
setlocale(LC_TIME, 'nl_NL');
$s = strftime('%#d %B %Y',$s);
This should output something like 13 January 2012.
I have a date/time string like this: 180510_112440 in this format ddmmyy_hhmmss
I need a snippet for having a string formatted like this way: 2010-05-18 11:24:40
Thanks for help.
another possible answer is the common use of strptime to parse your date and the mktime function:
<?php
$orig_date = "180510_112440";
// Parse our date in order to retrieve in an array date's day, month, etc.
$parsed_date = strptime($orig_date, "%d%m%y_%H%M%S");
// Make a unix timestamp of this parsed date:
$nice_date = mktime($parsed_date['tm_hour'],
$parsed_date['tm_min'],
$parsed_date['tm_sec'],
$parsed_date['tm_mon'] + 1,
$parsed_date['tm_mday'],
$parsed_date['tm_year'] + 1900);
// Verify the conversion:
echo $orig_date . "\n";
echo date('d/m/y H:i:s', $nice_date);
$inDate = '180510_112440';
$date = strtotime('20'.substr($inDate,4,2).'-'.
substr($inDate,2,2).'-'.
substr($inDate,0,2).' '.
substr($inDate,7,2).':'.
substr($inDate,9,2).':'.
substr($inDate,11,2));
echo date('d-M-Y H:i:s',$date);
Assumes date will always be in exactly the same format, and always 21st century
list($d,$m,$y,$h,$i,$s)=sscanf("180510_112440","%2c%2c%2c_%2c%2c%2c");
echo "20$y-$m-$d $h:$i:$s";