3

I face this problem to integrate bKash Online Payment Gateway API. The Documentation is here - https://developer.bka.sh/docs/create-payment-1.

In 'Create Payment' section bKash return paymentCreateTime string as following format.

2020-01-07T11:55:34:438 GMT+0600

How can I convert it to 2020-01-07 11:55:34 to save in MySQL?

I find a solution like following way.

$input     = "2020-01-07T11:55:34:438 GMT+0600"                   // "2020-01-07T11:55:34:438 GMT+0600"
$timestamp = substr($input,0,19);                                 // "2020-01-07T11:55:34"
$mysql     = date_format(date_create($timestamp),'Y-m-d H:i:s');  // "2020-01-07 11:55:34"

But I need a solution without substr() function.

Sanaulla
  • 1,329
  • 14
  • 13
  • Does this answer your question? [how to convert php date formats to GMT and vice versa?](https://stackoverflow.com/questions/5454779/how-to-convert-php-date-formats-to-gmt-and-vice-versa) – Ray Jan 07 '20 at 07:58
  • use Carbon::parse()->format(Y-m-d H:i:s); – LuongPs Jan 07 '20 at 08:13
  • 1
    @Raymond - The following link is not like a solution to convert '2020-01-07T11:55:34:438 GMT+0600' to date-time format. Need a specified code. – Sanaulla Jan 07 '20 at 08:14

2 Answers2

1

You can use createFromFormat() to directly parse your string.

Importantly, you'll need to escape T and GMT.

Everything else in https://www.php.net/manual/en/datetime.createfromformat.php

$input = '2020-01-07T11:55:34:438 GMT+0600';
$date = DateTime::createFromFormat('Y-m-d\Th:i:s:u \G\M\TO', $input);
echo $date->format('Y-m-d h:i:s');

This will cleanly deliver an object that you can format however you like.

Here is a demonstration:

$date = new DateTime("now");
echo $date->format('Y-m-d\Th:i:s:u \G\M\TO');

echo "\n---\n";

$input = '2020-01-07T11:55:34:438 GMT+0600';
echo $input;

echo "\n---\n";

$date = DateTime::createFromFormat('Y-m-d\Th:i:s:u \G\M\TO', $input);
echo $date->format('Y-m-d h:i:s');

echo "\n===\n";

var_dump($date);

Demonstration Output:

2020-01-07T10:12:53:000245 GMT+0100
---
2020-01-07T11:55:34:438 GMT+0600
---
2020-01-07 11:55:34
===
object(DateTime)#2 (3) {
  ["date"]=>
  string(26) "2020-01-07 11:55:34.438000"
  ["timezone_type"]=>
  int(1)
  ["timezone"]=>
  string(6) "+06:00"
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

You can use it like this

date_default_timezone_set("Asia/Dhaka");
$mysql1 = date_format(date_create(strtotime($input)),'Y-m-d H:i:s');

it will print 2020-01-07 09:17:23

Amit Sharma
  • 1,775
  • 3
  • 11
  • 20
  • Here, if I set $input = "2020-01-07T11:55:34:438 GMT+0600"; Then, strtotime($input) // false date_create(strtotime($input)) // date: 2020-01-07 14:23:31.486348 Asia/Dhaka (+06:00) That is the present time, not the $input variable time. – Sanaulla Jan 07 '20 at 08:25
  • Are we accommodating 3 digits of microseconds or 6 digits? – mickmackusa Jan 07 '20 at 08:27
  • updated the code. just set date_default_timezone_set("Asia/Dhaka"); at top of your code . it will give you current time – Amit Sharma Jan 07 '20 at 08:33
  • >>> $input => "2020-01-07T11:55:34:438 GMT+0600" >>> date_default_timezone_set("Asia/Dhaka"); => true >>> $mysql1 = date_format(date_create(strtotime($input)),'Y-m-d H:i:s'); => "2020-01-07 15:55:51" Sorry. This is not perfect result. – Sanaulla Jan 07 '20 at 09:57
  • then what you need ? – Amit Sharma Jan 07 '20 at 10:08