Jump to content

preg_replace mysql date


The14thGOD

Recommended Posts

I'm updating a database from a CSV file, for some reason the database isn't putting quotes around the dates, but it breaks if the data doesn't have quotes around it. Now I originally just targeted those fields in an array and added the quotes (e.g. $arr[22] = '\''.$arr[22].'\'';) and that worked until it got to a field with no date in it. That broke that so I had to go back to trying to just find a regex to add the quotes (and hope that the insert works if there is no date and no quotes).

 

Here's the regex I wrote (I'm by no means an expert on this) and it doesn't work so any help would be great;

 

<?php
	$data = preg_replace('^([0-9]{4}-[0-9]{2}-[0-9] [0-9]{2}:[0-9]{2}:[0-9]{2})^','\'\\1\'',$data);
?>

 

All dates (except ones that don't exist) follow that pattern since it's stored in a MySQL db.

Thanks for any and all help,

Justin

Link to comment
https://forums.phpfreaks.com/topic/179118-preg_replace-mysql-date/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.