Convert date string with timezone to timestamp - php

I receive dates in the following format 2015-01-09T20:46:00+0100 and need to convert it in timestamp.
Unfortunately, strtotime function is ignoring the timezone component :
print strtotime('2015-01-09T20:46:00+0100') . "\n";
print strtotime('2015-01-09T20:46:00');
//result is the same with or without timezone:
//1420832760
//1420832760
What is the right way to solve this issue ?

DateTime handles this correctly:
$date = new DateTime('2015-01-09T20:46:00+0100');
echo $date->getTimestamp();
echo "\n";
$date = new DateTime('2015-01-09T20:46:00');
echo $date->getTimestamp();
1420832760
1420836360
Demo

I fugured out !
uses the default time zone unless a time zone is specified in that
parameter
http://php.net/manual/en/function.strtotime.php
That's why the result is the same setting or not the timezone :
date_default_timezone_set('Europe/Paris');
print strtotime('2015-01-09T20:46:00+0200');
print "\n";
print strtotime('2015-01-09T20:46:00+0100');
print "\n";
print strtotime('2015-01-09T20:46:00');
print "\n\n";
date_default_timezone_set('UTC');
print strtotime('2015-01-09T20:46:00+0100');
print "\n";
print strtotime('2015-01-09T20:46:00');
output:
1420829160
1420832760
1420832760
1420832760
1420836360
Demo: https://eval.in/241781
Thanks for the help!

Related

Convert date and time to only year in php

I have a variable is which the value coming is Date along with time in php. How do I convert it into a variable to get only the year? I do not need automatic updation but the format change is needed. Normal answers are giving it about date but my variable is containing time as well.
The format coming by now is 2017-12-11 4:06:37 and i need only 2017
Use like this:
<?php echo date('Y',strtotime('now'));?>
You can you simple DateTime function and date_formate() function for displaying separate year, month and date.
For that you have to first convert in Object of your current Date time string by using :
$date = new \DateTime('2017-12-11 4:06:37');
And then you can use date format function by using below code:
echo date_format($date, "Y"); //for Display Year
echo date_format($date, "m"); //for Display Month
echo date_format($date, "d"); //for Display Date
You can code like this (working perfectly):
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y') . "\n";
As mentioned by Himanshu Upadhyay, this is correct and the easiest way.
<?php
echo date('Y',strtotime('now'));
?>
But i would recommend you to read this here. You should really do actually!
By using DateTime class
$date = new \DateTime('2017-12-11 4:06:37');
echo $date->format('Y');

get time in utc 2015-03-19T10:57:40.174Z format

I am trying to get time in 2015-03-19T10:57:40.174Z format but its not working for me
I have tried these-
1.
echo date('Y-M-DTHHM:SS.MMMZ');
2.
date_default_timezone_set('utc');
$datetime = new DateTime();
$str = $datetime->format(DateTime::ISO8601);
echo $str;
output-2015-03-19T17:27:46+0530
3.
date_default_timezone_set();
print date('c');
output-2015-03-19T17:33:52+05:30
but i need exact format(2015-03-19T10:57:40.174Z). how can i do that ?
please help.

strtotime returning wrong day

When using the following
echo date('D',strtotime("2013-06-16T06:00:00-07:00"));
echo date('D',strtotime("2013-06-16T18:00:00-07:00"));
First it returns Sun and the Second returns Mon. I'm not really sure why or how to correct! The Date:"2013-06-16T06:00:00-07:00" is data I'm retrieving from a XML file. The timestamp has the correction for UTC at the end not sure if this is generating the error.
Thanks for any help.
To get expected results you should consider using DateTime():
<?php
echo date('D',strtotime("2013-06-16T06:00:00-07:00")) . "\n";
echo date('D',strtotime("2013-06-16T18:00:00-07:00")) . "\n";;
$dt1 = new DateTime("2013-06-16T06:00:00-07:00");
$dt2 = new DateTime("2013-06-16T18:00:00-07:00");
echo $dt1->format('D') . "\n";
echo $dt2->format('D') . "\n";
Output
Sun
Mon
Sun
Sun
Fiddle
This is because The Date represents the time is in time zone specified in date.timezone settings. So the timezone -07:00 is parsed and converted back to date.timezone timezone.
To understand the idea just add e in the date string
echo date('D e',strtotime("2013-06-16T06:00:00-07:00"));
echo date('D e',strtotime("2013-06-16T18:00:00-07:00"));
See example.
Its better you use DateTime(). It does not have such limitation.

How to create a SQLite Timestamp value in PHP

I want to generate a timestamp in PHP and then store that value in SQLite.
Here is the code:
$created_date = date("YYYY-MM-DD
HH:MM:SS",time());
Here is what gets stored in the DB (which looks wrong):
11/21/0020 12:39:49 PM
How should I change my code to store the current date/time properly in SQLite
Why not just update your SQLite query to use date('now')? reference
But, within PHP you should be able to use
$created_date = date('Y-m-d H:i:s');
I just implemented this for my own project..
My solution was to use the UTC time format officially specified for the web: RFC3339.
You can use any of the following PHP time format constants:
DATE_RFC3339
DATE_W3C
DATE_ATOM
Example:
$sqlite_timestamp = date(DATE_RFC3339);
echo "My SQLite timestamp = ".$sqlite_timestamp;
The best part is the alphanumeric order corresponds to the date order, so you can use ORDER BY SQL statements on the date fields!
How do you check what the stored value is? Also, what is the datatype of the column in the database? Are you aware that SQLite is typeless, so all values are being stored as strings or integers?
$created_date = date('Y-m-d H:i:s');
echo $created_date ."<br />\n";
$created_date = date('l, F jS, Y - g:ia');
echo $created_date ."<br />\n";
$created_date = date('n/j/y H:i:s');
echo $created_date ."<br />\n";
$created_date = date('r'); // RFC 2822 formatted date
echo $created_date ."<br />\n";
$created_date = date('c'); // ISO-8601 formatted date
echo $created_date ."<br />\n";
Output could be:
2011-04-01 19:33:40
Friday, April 1st, 2011 - 7:33pm
4/1/11 19:33:40
Fri, 01 Apr 2011 19:33:40 +0200
2011-04-01T19:33:40+02:00
Be aware that date() function depends of default timezone set in PHP config. You can always test it using echo date_default_timezone_get(); or set it by date_default_timezone_set('TIMEZONE_IDENTIFIER'); where TIMEZONE_IDENTIFIER is one from the list of supported timezones.

Reformat a date in PHP

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";

Categories