I am trying to update all of the date fields in the database to add 5 days to each date in all tables.
I managed to pull the dates and row id into an array then generate a SQL code statement to update them. However, each table has a different date format some with time included some without. I want to add 5 days to a date then save it back. At the moment I can do this if all dates have the same format but that's not good enough to solve my problem.
What I want is a code that can generate string format from a date string. For example:
Date String 2014-12-04 I want the code to say this date has Y-m-d format. If date string is 2017-04-03 11:11:48.653 I want the code to say this date format is Y-m-d h:i:s.
If you don't need to preserve the format (i.e. you can change the format in the database while adding five days), you can just throw the string at strtotime. It will try and detect the format, if possible:
$timestamp = strtotime($string_with_unknown_format);
Alternatively, you can check for different formats with regex:
function extractDateTimeFormat($string) {
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $string))
return 'Y-m-d';
if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $string))
return 'Y-m-d H:i:s';
if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}$/', $string))
return 'Y-m-d H:i:s.v';
if (preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $string))
return 'm/d/Y';
if (preg_match('/^\d{2}\.\d{2}\.\d{4}$/', $string))
return 'd.m.Y';
}
However, that could get tiresome, depending on how many formats you expect. On the other hand - how many can there be?
The next alternative would be to replace the digits by their placeholders directly in the string - this would be more flexible, but possibly a little less predictable. In this case, the order of the lines is important. If you need to add 12 hour formats (AM / PM), you need to insert the lines prior to the line for H:i:s or it won't work.
function extractDateTimeFormat($string) {
$string = preg_replace('/\b\d{4}-\d{2}-\d{2}\b/', 'Y-m-d');
$string = preg_replace('/\b\d{2}\/\d{2}\/\d{4}\b/', 'm/d/Y');
$string = preg_replace('/\b\d{2}\.\d{2}\.\d{4}\b/', 'd.m.Y');
$string = preg_replace('/\b\d{2}:\d{2}\b:\d{2}\b/', 'H:i:s');
$string = preg_replace('/\b\d{2}:\d{2}\b/', 'H:i');
$string = preg_replace('/\.\d{3}\b/', '.v');
if (preg_match('/\d/', $string)
return false;
return $string;
}
That way, you'll detect date and time formats independently so you don't have to think of every possible combination.
You'll have to check with your live data which method works better.
The best way to format dates and know what date format, solution for those who are developing in date format. I also add the ISO8601 date format
function date_extract_format( $d, $null = '' ) {
// check Day -> (0[1-9]|[1-2][0-9]|3[0-1])
// check Month -> (0[1-9]|1[0-2])
// check Year -> [0-9]{4} or \d{4}
$patterns = array(
'/\b\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3,8}Z\b/' => 'Y-m-d\TH:i:s.u\Z', // format DATE ISO 8601
'/\b\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\b/' => 'Y-m-d',
'/\b\d{4}-(0[1-9]|[1-2][0-9]|3[0-1])-(0[1-9]|1[0-2])\b/' => 'Y-d-m',
'/\b(0[1-9]|[1-2][0-9]|3[0-1])-(0[1-9]|1[0-2])-\d{4}\b/' => 'd-m-Y',
'/\b(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])-\d{4}\b/' => 'm-d-Y',
'/\b\d{4}\/(0[1-9]|[1-2][0-9]|3[0-1])\/(0[1-9]|1[0-2])\b/' => 'Y/d/m',
'/\b\d{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])\b/' => 'Y/m/d',
'/\b(0[1-9]|[1-2][0-9]|3[0-1])\/(0[1-9]|1[0-2])\/\d{4}\b/' => 'd/m/Y',
'/\b(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])\/\d{4}\b/' => 'm/d/Y',
'/\b\d{4}\.(0[1-9]|1[0-2])\.(0[1-9]|[1-2][0-9]|3[0-1])\b/' => 'Y.m.d',
'/\b\d{4}\.(0[1-9]|[1-2][0-9]|3[0-1])\.(0[1-9]|1[0-2])\b/' => 'Y.d.m',
'/\b(0[1-9]|[1-2][0-9]|3[0-1])\.(0[1-9]|1[0-2])\.\d{4}\b/' => 'd.m.Y',
'/\b(0[1-9]|1[0-2])\.(0[1-9]|[1-2][0-9]|3[0-1])\.\d{4}\b/' => 'm.d.Y',
// for 24-hour | hours seconds
'/\b(?:2[0-3]|[01][0-9]):[0-5][0-9](:[0-5][0-9])\.\d{3,6}\b/' => 'H:i:s.u',
'/\b(?:2[0-3]|[01][0-9]):[0-5][0-9](:[0-5][0-9])\b/' => 'H:i:s',
'/\b(?:2[0-3]|[01][0-9]):[0-5][0-9]\b/' => 'H:i',
// for 12-hour | hours seconds
'/\b(?:1[012]|0[0-9]):[0-5][0-9](:[0-5][0-9])\.\d{3,6}\b/' => 'h:i:s.u',
'/\b(?:1[012]|0[0-9]):[0-5][0-9](:[0-5][0-9])\b/' => 'h:i:s',
'/\b(?:1[012]|0[0-9]):[0-5][0-9]\b/' => 'h:i',
'/\.\d{3}\b/' => '.v'
);
//$d = preg_replace('/\b\d{2}:\d{2}\b/', 'H:i',$d);
$d = preg_replace( array_keys( $patterns ), array_values( $patterns ), $d );
return preg_match( '/\d/', $d ) ? $null : $d;
}
function date_formating( $date, $format = 'd/m/Y H:i', $in_format = false, $f = '' ) {
$isformat = date_extract_format( $date );
$d = DateTime::createFromFormat( $isformat, $date );
$format = $in_format ? $isformat : $format;
if ( $format ) {
if ( in_array( $format, [ 'Y-m-d\TH:i:s.u\Z', 'DATE_ISO8601', 'ISO8601' ] ) ) {
$f = $d ? $d->format( 'Y-m-d\TH:i:s.' ) . substr( $d->format( 'u' ), 0, 3 ) . 'Z': '';
} else {
$f = $d ? $d->format( $format ) : '';
}
}
return $f;
} // end function
function date_convert_format( $old = '' ) {
$old = trim( $old );
if ( preg_match( '/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $old ) ) { // MySQL-compatible YYYY-MM-DD format
$new = $old;
} elseif ( preg_match( '/^[0-9]{4}-(0[1-9]|[1-2][0-9]|3[0-1])-(0[1-9]|1[0-2])$/', $old ) ) { // DD-MM-YYYY format
$new = substr( $old, 0, 4 ) . '-' . substr( $old, 5, 2 ) . '-' . substr( $old, 8, 2 );
} elseif ( preg_match( '/^(0[1-9]|[1-2][0-9]|3[0-1])-(0[1-9]|1[0-2])-[0-9]{4}$/', $old ) ) { // DD-MM-YYYY format
$new = substr( $old, 6, 4 ) . '-' . substr( $old, 3, 2 ) . '-' . substr( $old, 0, 2 );
} elseif ( preg_match( '/^(0[1-9]|[1-2][0-9]|3[0-1])-(0[1-9]|1[0-2])-[0-9]{2}$/', $old ) ) { // DD-MM-YY format
$new = substr( $old, 6, 4 ) . '-' . substr( $old, 3, 2 ) . '-20' . substr( $old, 0, 2 );
} else { // Any other format. Set it as an empty date.
$new = '0000-00-00';
}
return $new;
}
$date_1 = '13/05/2020 19:20:15.156457';
$date_2 = '25-05-2020 10:20';
$date_3 = '2020.05.20 10:20';
$date_4 = '2020.25.05 18:20';
$date_5 = '05/05/2020 12:20';
$date_6 = '05.05.2020 10:20';
$date_7 = '2020-20-05';
//-----------------------------
echo "1($date_1): " . date_formating( $date_1, false, true ) . PHP_EOL;
// echo-> isformat: d/m/Y H:i:s.u
// 1(13/05/2020 19:20:15): 2020-05-13 19:20
echo "2($date_2): " . date_formating( $date_2 ) . PHP_EOL;
// echo-> isformat: d-m-Y H:i
// 2(25-05-2020 10:20): 25/05/2020 10:20
echo "3($date_3): " . date_formating( $date_3 ) . PHP_EOL;
// echo-> isformat: Y.m.d H:i
// 3(2020.05.20 10:20): 20/05/2020 10:20
echo "4($date_4): " . date_formating( $date_4 ) . PHP_EOL;
// echo-> isformat: Y.d.m H:i
// 4(2020.25.05 18:20): 25/05/2020 18:20
echo "5($date_5): " . date_formating( $date_5 ) . PHP_EOL;
// echo-> isformat: d/m/Y H:i
// 5(05/05/2020 12:20): 05/05/2020 12:20
echo "6($date_6): " . date_formating( $date_6 ) . PHP_EOL;
// echo-> isformat: d.m.Y H:i
// 6(05.05.2020 10:20): 05/05/2020 10:20
echo "7($date_7): " . date_formating( $date_7, false, true ) . PHP_EOL;
// echo-> isformat: Y-d-m
// 7(2020-20-05): 2020-20-05
echo "Date ISO8601: = " . date_formating( $date_1, 'DATE_ISO8601' ) . PHP_EOL;
// echo-> isformat: d/m/Y H:i:s.u
// Date ISO8601: = 2020-05-13T19:20:15.156Z
echo "7($date_7): = " . date_convert_format( $date_7 );
// 7(2020-20-05): = 2020-20-05
I am trying to remove all dates from strings in PHP using preg_replace(). The dates are of the following formats: YYYY-MM-DD, YYYY/MM/DD or YYYY.MM.DD
$string1 = "Current from 2014-10-10 to 2015-05-23";
$output = preg_replace('/\d{4}[\/\-\.](0?[1-9]|1[012])[\/\-\.](0?[1-9]|[12][0-9]|3[01])/g', '', $string1);
Expected output is "Current from to ". Currently I am getting back "".
Any help greatly appreciated! Wonko
This should work.
$input = "Current from 2014-10-10 to 2015/05/23 and 2001.02.10";
$output = preg_replace('/(\d{4}[\.\/\-][01]\d[\.\/\-][0-3]\d)/', '', $input);
echo $output;
Update
To ensure that the date also is valid
<?php
$input = "Current from 2014-10-10 to 2015/05/23 and 2001.19.10";
$output = preg_replace_callback('/(\d{4}[\.\/\-][01]\d[\.\/\-][0-3]\d)/', function($matches) {
$date = str_replace(array('.','/'), '-', $matches[1]);
$newDate = DateTime::createFromFormat('Y-m-d', $date);
if($newDate->format('Y-m-d') == $date) {
return false;
}else {
return $matches[1];
}
}, $input);
echo $output;
I have two strings (read from an XML file), say '2014-10-17T11:17:44' and '2014-10-17T16:19:15'. I want to compare them using only the date part (without time). I already tried ...script below
if(file_exists($xmlFullFilename)) {
if($entryTimeNode->format("Y-m-d") = $entryTimeNode->format("Y-m-d")){
( appends to the exisiting file ....)
$xmlDoc = new DomDocument();
$tmp = split(" ", $entryTime);
$dateString = $tmp[0] . "T" . $tmp[1];
$entryTimeNode = $xmlDoc->createElement("EntryTime", $dateString);
}
else {
//create a new xml file .
}}
XML FILE
<Incidents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Count="2" Date="2014-10-24" Time="12:05:39" FileName="2014-10-24_Brook_Retail_Park.xml">
<Incident>
<EntryTime>2014-10-17T11:17:44</EntryTime>
</Incident>
<Incident>
<EntryTime>2014-10-17T16:19:15</EntryTime>
</Incident></Incidents>
My soluction is
$date = "2014-10-17T11:17:44";
$date2 = "2014-10-17T16:19:15";
$date = new DateTime($date);
$date2 = new DateTime($date2);
$diff = $date->diff($date2);
if((int)$diff->format('%y%m%d')==0)
{
// Dates are equal
}
I have two PHP variable $consult_date_arr and $consultationdate.the user can insert any date format consider for now 22-12-2014 or 22/12/2014.what i want to do is i want to write a explode function which should work for both the condition i.e. for both the date formats.
$consult_date_arr=explode("/",$consultationdate);
$consult_date_arr=explode("-",$consultationdate);
The explode function should be the combination of above two explode function in functionality,such date it works for both the formats.if any doubt please let me know.
thanks in advance.
try something like this
<?php
$date = '22/12/2014';
$keywords = preg_split("/[-,\/]+/", $date);
print_r($keywords);
?>
preg_split — Split string by a regular expression
REFERENCE
http://php.net/manual/en/function.preg-split.php
<?php
$dt = "1992-05-30";
//$dt = "1992/05/30";
function expld($date){
$date = str_replace("/", "-", $date);
$date = explode("-",$date);
return $date;
}
$newdate = expld($dt);
echo $newdate[0];
?>
you can write custom function and call explode inside it
like
$string = "-" OR "/" Or whatever you want to explode with
function dateFormat($string,$date)
{
$consult_date_arr=explode($string,$consultationdate);
return $consult_date_arr;
}
you will need regular expression, using preg_match in php.
<?php
$consult_date_arr;
$consultationdate = '22/12/2014';
if(preg_match('/[\d]{2}-[\d]{2}-[\d]{4}/', $consultationdate)){
$consult_date_arr = explode('-', $consultationdate);
}else if(preg_match('/[\d]{2}\/[\d]{2}\/[\d]{4}/', $consultationdate)){
$consult_date_arr = explode('/', $consultationdate);
}
var_dump($consult_date_arr);
?>
Hi check the code below,
<?php
if(!function_exists('date_explode')){
function date_explode($date){
$explode_date = array();
$delimiter = '';
if(strstr($date, '/')){
$delimiter = '/';
}elseif(strstr($date, '-')){
$delimiter = '-';
}
if(!empty($delimiter)){
$explode_date = explode($delimiter, $date);
}
return $explode_date;
}
}
$consult_date_arr=date_explode('22-12-2014');
print_r($consult_date_arr);
$consult_date_arr=date_explode('22/12/2014');
print_r($consult_date_arr);
?>
fiddle example
P.S.: Please do validation its a must, validation will prevent you from this type of issues
I am trying to parse a Google Calendar to use on our TV's to display 'Today's Events'.
While I am most of the way there thanks to the help of a friend, I wanted to see if somebody could help me the rest of the way.
The code below generates the calendar with all the information, but for EVERY entry it shows the date. Since they are all the same day, this is kind of frustrating and confusing when looking at it. I am nowhere near a programmer, but I can make sense of some things.
How would I group all Todays events under a single date heading?
Thanks in advance.
<?php
$confirmed = 'http://schemas.google.com/g/2005#event.confirmed';
$three_months_in_seconds = 60 * 60 * 24 * 28 * 3;
$three_months_ago = date("Y-m-d\Th:i:s", time() - $three_months_in_seconds);
$three_months_from_today = date("Y-m-d\Th:i:s", time() + $three_months_in_seconds);
$params = "?orderby=starttime&start-min=" . $three_months_ago . "&start-max=" . $three_months_from_today;
//$params = "?orderby=starttime&start-min=2012-12-01T05:48:47&start-max=2013-05-07T05:48:47&sortorder=a&singleevents=true&futureevents=true";
$params = "?orderby=starttime&sortorder=a&singleevents=true&futureevents=true";
$feed = "https://www.google.com/calendar/feeds/REDACTED%40gmail.com/private-REDACTED/full".$params;
$doc = new DOMDocument();
if (!$doc->load( $feed )) echo 'failed to load';
$entries = $doc->getElementsByTagName( "entry" );
foreach ( $entries as $entry ) {
$status = $entry->getElementsByTagName( "eventStatus" );
$eventStatus = $status->item(0)->getAttributeNode("value")->value;
if ($eventStatus == $confirmed) {
$titles = $entry->getElementsByTagName( "title" );
$title = $titles->item(0)->nodeValue;
$times = $entry->getElementsByTagName( "when" );
$startTime = $times->item(0)->getAttributeNode("startTime")->value;
$when = date( "D M j, Y", strtotime( $startTime ) );
$time = date("g:i A",strtotime($startTime));
$places = $entry->getElementsByTagName( "where" );
$where = $places->item(0)->getAttributeNode("valueString")->value;
print "<div class='row when'>$when</div>";
echo "<div class='row event'><span class='time'>$time</span><span class='title'>$title</span><span class='where'>$where</span></div>";
// print $where . "\n";
print "\n";
}
}
?>
Have an answer:
just change this:
print "<div class='row when'>$when</div>";
to this:
if ($old_when!=$when) print "<div class='row when'>$when</div>"; $old_when=$when;
and add
$old_when = null;
before the foreach