Convert date format in PHP to unix - php

I have my date in the following format:
01-02-2019
and I need to get it into the following format:
2019-02-01T00:00:00.000
Is it strtotime I need? I find that a bit confusing to use.
strtotime("01-02-2019");
Thank you for your help.

As #Vidal said, it's look like ISO 8601 but it's a bit different on your example.
If this is the exact result you need, here's how:
echo \DateTime::createFromFormat('d-m-Y','01-02-2019')->format('Y-m-d\TH:i:s.v');
will display : 2019-02-01T17:38:33.000
More about the format parameters: http://php.net/manual/en/function.date.php
Edit: I would recommend Vidal answer if this exact format is not mandatory since it's respecting an ISO norme.

I think the format you want is ISO 8601.
Here the php code.
<?php
$unixTimestamp = date("c", strtotime('01-02-2019'));
?>

As Vidal said in their answer, I think the ISO 8601 standard is what you're after.
I personally prefer the OOP approach, and would recommend always using PHP's DateTime() class over strtotime(), but either will do what you're looking for.
To do the same formatting with DateTime, simply instantiate the DateTime object, and format it as you would with strtotime() like so:
// Build a date object
$myDate = new DateTime('01-02-2019');
// Format and display in the ISO 8601 standard using
// the 'c' format option.
echo $date->format('c');
The magic here is in the 'c' format string, which represents the ISO 8601 constant available to the DateTime class.
More info on DateTimeInterface constants

You can do this way if you want to convert it to another format
<?php
// Create a new DateTime object
echo date('Y-m-d\TH:i:s.v',strtotime('01-02-2019'));
?>
OR
<?php
// Create a new DateTime object
$date = DateTime::createFromFormat('d-m-Y', '01-02-2019');
echo $date->format('Y-m-d\TH:i:s.v');
?>
DEMO1: https://3v4l.org/IlCur
DEMO2: https://3v4l.org/6CMKT

Related

PHP convert string into date time

I have a php string from db it is 20/11/2017 I want to convert it milliseconds.
It's my code to doing that.
$the_date = "20/11/2017";
$mill_sec_date = strtotime($the_date);
var_dump($mill_sec_date);
But it does not print any thing rather than
bool(false);
What is the problem and how can i solve it ????
When using slashes to separate parts of the date, PHP recognizes the format as MM/DD/YYYY. Which makes your date invalid because there is no 20th month. If you want to use the format where day and month is swapped, you need to use hyphens, like DD-MM-YYYY.
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
print_r($newformat);
Use DateTime class to call function createFromFormat
$date = date_create_from_format('d/M/Y:H:i:s', $string);
$date->getTimestamp();
Most likely you got the date format wrong, see
here for a list of supported date and time formats:
This section describes all the different formats that the strtotime(), DateTime and date_create() parser understands.
You string is not accept by the strtotime, you can use createFromFormat set set the with the format type of the time string like below, you can also check the live demo. And you also can refer to this answer
var_dump(DateTime::createFromFormat('d/m/Y', "20/11/2017"));

PHP ISO 8601 format conversion issue

I want to convert this string '2016-05-30 15:35:00' into ISO 8601 format which should look like this:
Expected result : 2016-05-30T15:35:00+05:30
I tried following, but in the output it's 2 hours less,
$phpdate = strtotime('2016-05-30 15:35:00');
echo gmdate('Y-m-d\TH:i:s\+05:30', $phpdate);
Output : 2016-05-30T13:35:00+05:30
Have I made a mistake somewhere? or is there an easier way for this conversion?
You're using gmdate function of PHP which converts date into UTC format instead you need to use date function like as
date_default_timezone_set("Asia/Kolkata");//Set your timezone to Asia/Kolkata
$phpdate = strtotime('2016-05-30 15:35:00');
echo date('c', $phpdate);// 2016-05-30T13:35:00+05:30 if your timezone is of Asia/Kolkata
for an easier way to handle dates i recommend Carbon
for your problem it would be :
$date = new Carbon("2016-05-30 15:35:00");
echo $date->toIso8601String();

How to convert yyyy-MM-ddTHH:mm:ssZ to yyyy-MM-dd HH:mm:ss?

Paypal returns a timestamp of the following format:
yyyy-MM-ddTHH:mm:ssZ
And I don't quite know what to do with it...
How can I convert it to yyyy-MM-dd HH:mm:ss using my local timezone in php?
I'm tempted to preg_replace the mysterious letters, but something tells me there must a better way. There also appears to be 8 hours difference to my zone which I'm not sure how to substract.
Use DateTime class to do your magic.
$date = new DateTime('2012-09-09T21:24:34Z');
$date->format('Y-m-d'); # read format from date() function
You can use strtotime() to get a UNIX timestamp. From there you can do whatever you need: DateTime object, date(), etc.
Example with date():
echo date('r', strtotime('2012-09-10T10:00:00Z'));

Formatting datetime

i am trying to format a datetime which comes fromt he database in the format of
2012-06-11 21:39:54
However i want it to display in the format of June 11
How can do this?
Thanks
echo date('M d', strtotime('2012-06-11 21:39:54'));
Output
You can also use DateTime object.
$date = new DateTime($yourString);
$date->format($yourFOrmat);
I think that it would be the best way because DateTime is really more powerful than timestamp and date/strtotime functions.
From the code I gave above you can add functionalities like modifying dates, iterate over the time, compare 2 dates without functions like str_to_time...
$date->modify('+1 day');//the day after for example
foreach(new DatePeriod($date,new DateInterval('PT1M'),10){
$date->format($yourFormat);//iterate each minute
}
and so on
PHP manual gives an excellent documentation about using Date/Time functions. Basically you will need a combination of two functions: strtotime() and date().
strtotime() will convert your date into Unix timestamp which can be supplied to date() as second argument.
The format of date you will need is: M d.
Alternative: In addition you could also try the MYSQL counterpart which won't require conversion to UNIX timestamp. It is documented here. Assuming you are using date as your Datetime field, you will need something like this,
SELECT id,..,DATE_FORMAT(`date`, '%M %d') as f_date FROM table
For formatting date using php, you need to pass timestamp of date
and format specifiers as arguments into date function .
Eg echo date('M d',strtotime('2012-06-11 21:39:54'));

How do I convert datetime to ISO 8601 in PHP

How do I convert my time from 2010-12-30 23:21:46 to ISO 8601 date format? (-_-;)
Object Oriented
This is the recommended way.
$datetime = new DateTime('2010-12-30 23:21:46');
echo $datetime->format(DateTime::ATOM); // Updated ISO8601
Procedural
For older versions of PHP, or if you are more comfortable with procedural code.
echo date(DATE_ISO8601, strtotime('2010-12-30 23:21:46'));
After PHP 5 you can use this: echo date("c"); form ISO 8601 formatted datetime.
http://ideone.com/nD7piL
Note for comments:
Regarding to this, both of these expressions are valid for timezone, for basic format: ±[hh]:[mm], ±[hh][mm], or ±[hh].
But note that, +0X:00 is correct, and +0X00 is incorrect for extended usage. So it's better to use date("c"). A similar discussion here.
How to convert from ISO 8601 to unixtimestamp :
strtotime('2012-01-18T11:45:00+01:00');
// Output : 1326883500
How to convert from unixtimestamp to ISO 8601 (timezone server) :
date_format(date_timestamp_set(new DateTime(), 1326883500), 'c');
// Output : 2012-01-18T11:45:00+01:00
How to convert from unixtimestamp to ISO 8601 (GMT) :
date_format(date_create('#'. 1326883500), 'c') . "\n";
// Output : 2012-01-18T10:45:00+00:00
How to convert from unixtimestamp to ISO 8601 (custom timezone) :
date_format(date_timestamp_set(new DateTime(), 1326883500)->setTimezone(new DateTimeZone('America/New_York')), 'c');
// Output : 2012-01-18T05:45:00-05:00
If you try set a value in datetime-local
date("Y-m-d\TH:i",strtotime('2010-12-30 23:21:46'));
//output : 2010-12-30T23:21
According to PHP offcial documentation you can simply format it to:
echo $objDateTime->format('c'); // ISO8601 formated datetime
echo $objDateTime->format(DateTime::ISO8601); // Another way to get an ISO8601 formatted string
ISO 8601 is basically represented in PHP as "Y-m-d\TH:i:sP"
You can get this value from a constant:
DateTime::ATOM - for PHP versions below 7.2 (was removed)
DateTimeInterface::ATOM - for PHP versions since 7.2
$datetime->format('Y-m-d\TH:i:s.u\Z') should give the proper format, with the "T" separator, "Z" timezone (make sure to convert to UTC first) and microseconds (omit .u if you don't intend to support fractional seconds).
See https://stackoverflow.com/a/9532375/65387 for discussion why should use T
You can try this way:
$datetime = new DateTime('2010-12-30 23:21:46');
echo $datetime->format(DATE_ATOM);
You can also get your timestamps conversion via mutation inside modal like this
class YourModal extends Model
{
public function getCreatedAtAttribute($date)
{
return date(DATE_ISO8601, strtotime($date)); // ISO 8601 Date Format
}
}

Categories