0

I have the following code:

$input = '2018-10-28T08:36:31.521Z';
$dateTime = preg_split('[T.]', $input);
echo ($dateTime[1]);

The output is:

08:36:31.521Z

the split with 'T' works, but dosen't work with '.' (The point)

I tried:

'[T\.]'

it dosen't work also (it dosen't ever split it).

Alessandro
  • 900
  • 12
  • 23
Slimane MEHARZI
  • 334
  • 5
  • 11
  • What does the expected output array look like ? – Madhur Bhaiya Oct 28 '18 at 09:06
  • 1
    Why are you using regular expressions to manipulate a date? What are you *actually* trying to do? – iainn Oct 28 '18 at 09:12
  • 1
    Possible duplicate of [Convert one date format into another in PHP](https://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – iainn Oct 28 '18 at 09:13
  • I expect to get: the date, then time, i dont need '.521Z', i wish getting ['', '', ''] ,then i continue using the first and the secon columuns – Slimane MEHARZI Oct 28 '18 at 09:14

2 Answers2

2

Regular expression should start and finish with particular characters. My advice is always develop with settings display_errors = 1 and error_reporting(E_ALL);

$input = '2018-10-28T08:36:31.521Z';
$dateTime = preg_split('/[T.]/', $input);
echo ($dateTime[1]);

your code should work, you just missed / in regular expression

Alex Kapustin
  • 1,869
  • 12
  • 15
1

For preg_split() function, the pattern must be surrounded by slash (or other character that fits the needs). For exemple :

 $input = '2018-10-28T08:36:31.521Z';
 $dateTime = preg_split('/[T.]/', $input);

 echo ($dateTime[1]);

Output :

08:36:31

But for datetime manupulation, you'd better use PHP DateTime object.

Hope it helps.

JazZ
  • 4,469
  • 2
  • 20
  • 40