Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i replace some things in my array?

I need to format a date in my array - but the date in the array isn't saved as a datetime in a database or something like this.. I've got the dates from my server with cut them out.

So I need to work with preg_replace or with str_replace

What I've tried so far using str_replace:

    $reverse_date = str_replace( '[', '' ,$reverse_date);
    $reverse_date = str_replace( ']', '' ,$reverse_date);
    $reverse_date = str_replace( '/', '.' ,$reverse_date); 

but I don't want to use three lines for this.

If I print_r this, I will get : 12.Oct.2015:01:10:43 +0200

before it was looking like this : [12/Oct/2015:00:37:29 +0200]

so this is okay ! But I still don't want to use three lines for this, but I don't understand the preg_replace syntax

I want the following output :

12.Oct.2015(space)01:10:43 +0200
like image 999
Evolution48 Avatar asked Dec 27 '25 18:12

Evolution48


1 Answers

As you have said you were getting a date from an array within the following format

[12/Oct/2015:00:37:29 +0200]

So instead of using str_replace or preg_replace you can simply use DateTime::createFromFormat function of PHP like as

$date = DateTime::createFromFormat("[d/M/Y:H:i:s P]","[12/Oct/2015:00:37:29 +0200]");
echo $date->format('d.M.Y H:i:s P');//12.Oct.2015 00:37:29 +02:00

Demo

like image 180
Narendrasingh Sisodia Avatar answered Dec 30 '25 08:12

Narendrasingh Sisodia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!