How do I convert different formatted dates in timestamp in php ? I have date in this August 2nd, 2015 format and I need to conver it to timestamp so that I can make comparisons. Thanks in advance.
<?php
echo $test = strtotime("August 2nd, 2015");
echo date("Y-m-d",$test);
?>
So use strtotime("August 2nd, 2015");
strtotime() can and will bite you if you use it with date formats that are unclear, eg: 11/10/12.
Learn to love DateTime:createFromFormat() and be explicit in your parsing formats.
var_dump(DateTime::createFromFormat("d/y/m", "11/10/12"));
object(DateTime)#1 (3) {
["date"]=>
string(26) "2010-12-11 10:02:42.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(17) "America/Vancouver"
}
Related
I have an example value of : 20160530105130
Which I want to convert to a datetime.
I have tried Carbon::createFromFormat('Ym',$value)
But that just errors.
I also tried with timestamp, but again error.
Anyone have an idea how I can achive this?
Create a DateTime or Carbon object from the string and use the year and month there.
$str = '20160530105130';
$dt = DateTime::createFromFormat('!Ym????????',$str);
var_dump($dt);
//object(DateTime)#2 (3) { ["date"]=> string(26) "2016-05-01 00:00:00.000000"
Demo: https://3v4l.org/bOJdU
With carbon:
$str = '20160530105130';
$dt = Carbon::createFromFormat('!Ym????????',$str);
echo $dt; //2016-05-01 00:00:00
Alternatively, the string can also be completely parsed with DateTime. The day and time can then be set to the desired values using the modify method. This variant makes it easier to read what is being done.
$str = '20160530105130';
$dt = date_create($str)->modify('first day of this month 00:00');
//object(DateTime)#2 (3) { ["date"]=> string(26) "2016-05-01 00:00:00.000000"
Carbon::createFromFormat('YmdHis', "$value")
Below is the time string I have,
'2019-12-30T15:42:33.891+11:00'
I tried to convert this to date format using below php code.
date('Y-m-d H:i:s',strtotime('2019-12-30T15:42:33.891+11:00'));
And i am getting 2019-12-30 10:12:33 as the output. It seems date getting correctly but time not.
How to display the time also correctly? Thanks in advance!
This date and time format relates to the "Zulu time" (UTC). UTC is refereed as Universal Time Coordinated. It is also known as “Z time” or “Zulu Time”.
You should change your timezone by some command like below code:
$date = new DateTime('2019-12-30T15:42:33.891+11:00', new DateTimeZone('UTC'));
echo $date->format('Y-m-d H:i:s');
Or, for having more tools you can use the Carbon library.
Your time string '2019-12-30T15:42:33.891+11:00' contains time zone information of +11:00.
With date() the date and time are converted into the local time zone of your server. For example, I get "2019-12-30 05:42:33" (Timezone Europe/Berlin).
You can set a different time zone with date_default_timezone_set() or use DateTime to output the time for a location with the time zone "+11:00".
echo date_create('2019-12-30T15:42:33.891+11:00')->format('Y-m-d H:i:s');
//2019-12-30 15:42:33
strtotime () returns an integer timestamp. The Iformation of the Timezone +11:00 are lost in the process, but are processed when the time stamp is determined.
The DateTime object always has a time zone.
echo '<pre>';
var_dump(date_create('2019-12-30T15:42:33.891+11:00'));
Output:
object(DateTime)#1 (3) {
["date"]=>
string(26) "2019-12-30 15:42:33.891000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+11:00"
}
Note (Update):
When creating a DateTime object, the optional parameter for the time zone is only taken into account if the time string does not contain a time zone!
var_dump(new DateTime('2019-12-30T15:42:33.891 +11:00', new DateTimeZone('UTC')));
object(DateTime)#1 (3) {
["date"]=>
string(26) "2019-12-30 15:42:33.891000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+11:00"
}
//without +11:00
var_dump(new DateTime('2019-12-30T15:42:33.891', new DateTimeZone('UTC')));
object(DateTime)#1 (3) {
["date"]=>
string(26) "2019-12-30 15:42:33.891000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
I need help with PHP DateTime with TimeZone converting to MongoDB\BSON\UTCDateTime.
If I have string "2015-10-20T04:02:00.608000+01:00" it gives me a DateTime
$date = DateTime::createFromFormat( 'Y-m-d\TH:i:s.uT', $string );
DateTime
date => "2015-10-20 04:02:00.608000"
timezone_type => 1
timezone => "+01:00"
If I convert it to MongoDB\BSON\UTCDateTime and convert back to PHP DateTime
$mDate = new \MongoDB\BSON\UTCDateTime( $date->format('U') * 1000 );
$mDate->toDateTime()->setTimeZone(new DateTimeZone('Europe/Bratislava'))
I will get a correct result 05:02
DateTime
date => "2015-10-20 05:02:00.000000"
timezone_type => 3
timezone => "Europe/Bratislava"
BUT if the input string has +02:00 TimeZone "2015-10-20T04:02:00.608000+02:00" and use the same approach the result is
DateTime
date => "2015-10-20 04:02:00.000000"
timezone_type => 3
timezone => "Europe/Bratislava"
Why is the second result 04:02 if I expect 06:02?
The response is correct. When you are adding a + timezone onto the timestamp, you are, in effect going East from UTC. You are not adding to the UTC time, you are actually subtracting from what the time is in UTC. This means that 2015-10-20T04:02:00.608000+01:00 is 3am UTC. 2015-10-20T04:02:00.608000+02:00 is 2am UTC. You can see this more easily if you keep going higher with your timezone offsets
$date = DateTime::createFromFormat( 'Y-m-d\TH:i:s.uT', "2015-10-20T04:02:00.608000+01:00");
$mDate = new \MongoDB\BSON\UTCDateTime( $date->format('U') * 1000 );
var_dump($date, $mDate->toDateTime());
object(DateTime)#1 (3) {
["date"]=>
string(26) "2015-10-20 04:02:00.608000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+01:00"
}
object(DateTime)#3 (3) {
["date"]=>
string(26) "2015-10-20 03:02:00.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
$date = DateTime::createFromFormat( 'Y-m-d\TH:i:s.uT', "2015-10-20T04:02:00.608000+02:00");
$mDate = new \MongoDB\BSON\UTCDateTime( $date->format('U') * 1000 );
var_dump($date, $mDate->toDateTime());
object(DateTime)#1 (3) {
["date"]=>
string(26) "2015-10-20 04:02:00.608000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+02:00"
}
object(DateTime)#3 (3) {
["date"]=>
string(26) "2015-10-20 02:02:00.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
MongoDB stores the UTC timestamp. When you are adding the Europe/Bratislava timezone, you are saying, "What time is in Bratislava for this UTC timestamp". For October (daylight savings time), it's a 1 hr difference.
On a side note. Try to never mix +XXXX and Unicode/Olson timezones (Europe/Bratislava). You will wind up with some very strange errors due to daylight savings time. If you need to record a user's local time to display back at some point, Create your DateTime objects with the optional 3rd parameter like:
$customerTz = 'Europe/Bratislava';
$date = DateTime::createFromFormat( 'Y-m-d\TH:i:s.u', $dateString, $customerTz);
Also check to see if you really need to create a DateTime at all, or just a new UTCDateTime directly with a timestamp and deal with the tz's in the display logic.
Good afternoon...
I try to make the difference in years between the current date and a date in the database like this:
$final = '2016-05-10'; //date database
$hoy = \Carbon\Carbon::now(); //today
$fecha_ingreso = \Carbon\Carbon::createFromFormat('Y-m-d', $final);
$diff = $hoy->diffInYears($fecha_ingreso); //diff
But when doing a var_dump() of $fecha_ingreso it shows me the following:
object(Carbon\Carbon)#259 (3) { ["date"]=> string(26) "2155-05-10 22:27:09.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }
The day and the month are ok but the year is not, why can this error be ??
Thank you very much for the help.
I assume you want the difference in number of years, try this.
$dbDate = \Carbon\Carbon::parse('2016-05-10');
$diffYears = \Carbon\Carbon::now()->diffInYears($dbDate);
// $diffYears is 1
This code :
$timestamp = 2016-10-06T09:50:54.000Z;
How do I separate or convert from the timestamp into a date and time?
example :
TimeStamp 2016-10-06T09:50:54.000Z
Date 2016-10-06
Jam(GMT +7) 04:55:45
Please help to solve this problem. Thanks
$d = new DateTime('2016-10-06T09:50:54.000Z');
echo $d->format('Y-m-d\TH:i:s.u'); // 2016-10-06T09:50:54.000000
echo $d->format('Y-m-d'); // 2016-10-06
// convert to GMT+7 timezone
$d->setTimezone(new DateTimeZone('GMT+7'));
echo $d->format('h:i:s'); // 04:50:54
<?php
$timestamp = "2016-10-06T09:50:54.000Z";
$datetime = new Datetime('2016-10-06T09:50:54.000Z');
echo "Date".$datetime->format('Y-m-d')."<br>";
echo "Jam".$datetime->format('H:i:s');
?>
Take a look at this short example:
<?php
$datetime = new Datetime('2016-10-06T09:50:54.000Z');
var_dump($datetime->format('Y-m-d H:i:s'));
The output obviously is: string(19) "2016-10-06 09:50:54"
Using different format strings you can convert your date time object into whatever representation you want to. 'Y-m-d' will give the date in international format for example.
Take a look at the great documentation: http://php.net/manual/en/class.datetime.php
Not much of a mystery:
<?php
$timestamp = '2016-10-06T09:50:54.000Z';
$dt = new DateTime($timestamp);
var_dump($dt);
$dt->setTimezone(new DateTimeZone('Asia/Jakarta'));
var_dump($dt);
object(DateTime)#1 (3) {
["date"]=>
string(26) "2016-10-06 09:50:54.000000"
["timezone_type"]=>
int(2)
["timezone"]=>
string(1) "Z"
}
object(DateTime)#1 (3) {
["date"]=>
string(26) "2016-10-06 16:50:54.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(12) "Asia/Jakarta"
}
Unlike strings, giving format to a proper date is trivial.
I don't know what time zone Jam is but I suggest you assign a city so you can account for DST (I've used Asia/Jakarta in my sample code, the complete list can be found at List of Supported Timezones). Just relying on a UTC offset like +7 may make your code unreliable during the summer.