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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.