Extract date from string + PHP - php

Hello I am tring to get date "8 January 2014" from given string.
Wednesday, 8 January 2014 at 17:40
It will be good if you can use preg_replace?

PHP strtotime is bad at understanding the word "at" so this code should work and not return 1970:
$date = date("d F Y", strtotime(str_replace('at ','',"Wednesday, 8 January 2014 at 17:40")));
echo $date;
You can change the format just as you want; just change the letter d F Y and the list of letter that returns different values can be found in the manual.

Just made solution with explode() function:
$str = "Wednesday, 8 January 2014 at 17:40";
$ex1 = explode(", ", $str);
$ex2 = explode(" at", $ex1[1]);
$value = $ex2[0];
echo $value;

The DateTime class makes your life simpler !
<?php
$date = DateTime::createFromFormat('l, j F Y \a\t G:i','Wednesday, 8 January 2014 at 17:40');
echo $date->format('j F Y'); //"prints" 8 January 2014

Try regex pattern like this:
$str = 'Wednesday, 8 January 2014 at 17:40';
$date = preg_replace('/\w+,\s([a-zA-Z0-9\s]+)\sat\s([0-9:]+)/i', '$1', $str);
echo $date;

Here is simple code to get 8 January 2014 for you :)
<?php
$txt = 'Wednesday, 8 January 2014 at 17:40';
preg_match('#(?<=,\s)\d{1,2}\s\w+\s\d{1,4}(?=\sat)#', $txt, $result);
?>

Related

PHP- strtotime not converting properly

I don't understand why strtotime is returning a date off by a few days.
$expiration = '2015-07-19'; // yyyy-mm-dd
$exp = strtotime($expiration);
$exp = date('F m, Y',$exp);
echo $exp; // returns "July 07, 2015" (NOT the 19th)
What am I missing?
UPDATE: Even if I do this:
echo date('F m, Y');
It says it's August 8 when today is August 21! Why!?!?!?
Here it is: Outputs July 19, 2015. See demo. You are using two characters to represent months in your date function. F and m are textual and numeric representations of a month. So change 'm' to 'd' to represent days. date("F d, Y") e.g. January 01, 2000
$expiration = '2015-07-19'; // yyyy-mm-dd
$exp = strtotime($expiration);
$exp = date('F d, Y',$exp);
echo $exp;
DEMO
Try this
$expiration = '2015-07-19'; // yyyy-mm-dd
$exp = strtotime($expiration);
//$exp = date('F m, Y',$exp);
$exp = date('jS F, Y',$exp);
Output
19th July, 2015

strtotime not converting correct year [duplicate]

This question already has an answer here:
php strtotime() messes with date of a different year
(1 answer)
Closed 9 years ago.
I am trying to convert a date using pickadate.js mixed with PHP using Laravel 4. The issue I am having is when I attempt to save dates that are > 2013. If I choose Jan 23th 2014 it will save as Jan 23th 2013.
This is what's being sent via $_POST to the date variable.
23 January, 2014
This is my setup
$date = strtotime($scheduler['date']);
Converts to UNIX 1358993640 (which reads Jan 23 2013)
$dateFormat = date('l: F d, Y',$date);
Which becomes:
Wednesday: January 23, 2013
Is there another function I could use? Or do I need to convert the time another way before strtotime? It works as long as it's 2013. So I am thinking once it hits 2014 it will work then also.
Assuming the textual date was created according to a fixed format, you can use DateTime::createFromFormat instead:
$date = DateTime::createFromFormat('d F, Y', $scheduler['date']);
echo $date->format('l: F d, Y');
Alternatively, just omit the comma:
echo date('l: F d, Y', strtotime('23 January 2014'));
Very strange. But found a fix. Testing on my local setup running PHP 5.5, it seems that the comma is what is causing the issue. So stripping out commas from the entered data produces the desired results with your test code:
// Set the test data.
$test_data = '23 January, 2014';
// Filter out commas from the '$test_data'
$test_data = preg_replace('/,/', '', $test_data);
// Get the Unix datetime from the test data.
$date = strtotime($test_data);
// Format the Unix datetime.
$dateFormat = date('l: F d, Y', $date);
// Output for debugging.
echo 'date: ' . $date . '<br />';
echo 'dateFormat: ' . $dateFormat . '<br />';
The output I get is:
date: 1390453200
dateFormat: Thursday: January 23, 2014

Get the day from a very simple date

Is there a simple way to get the day from a very simple date string?
For example:
14 mar 2012
gets converted to:
Wed, 14 mar 2012
You should familiarize yourself with DateTime:
$str = "14 mar 2012";
$date = DateTime::createFromFormat("j M Y", $str);
$str = $date->format("D, j M Y");
$dateUnformated = '14 mar 2012';
$dateFormated = date("D, j M Y", strtotime($dateUnformated));

Converting string to formatted date using PHP

I've the following string:
$str = "Tuesday, February 21 at 7:30am at Plano B";
The at Plano B is optional. I would like to convert it to: TUE 21 FEB 07:30
$str = "Tuesday, February 21 at 7:30am at Plano B";
$time = strtotime(trim(substr($str,0,(strrpos("at"))));
echo "Date: " . strtoupper(date('D d M H:i', $time));
What do you mean by "at Plano B is optional". Is it sometimes there, sometimes not?
Otherwise:
$str = "Tuesday, February 21 at 7:30am at Plano B";
preg_match("/[a-z]+, ([a-z]+ [0-9]{1,2}) at ([0-9]{1,2}:[0-9]{1,2}[am|pm])/i", $str, $match);
$time = strtotime($match[1] + ' ' + $match[2]);
echo "Date: " . strtoupper(date('D d M H:i', $time));
Is it always either "Plano B" or empty? or can it also be "Plano A" or something completely diffrent?
See here: http://regexr.com?2vvuj
But you are missing the year in the initial string, so can't parse as strtotime. Also you want output without am/pm.. Do you want to use 24 hour time?
This is not a pretty way, but without the year, i dont think we have much choice..
preg_match("/([a-z]+), ([a-z]+) ([0-9]{1,2}) at ([0-9]{1,2}:[0-9]{1,2})([am|pm])/i", $str, $match);
$day = substr($match[1], 0, 3);
$mon = substr($match[2], 0, 3);
echo strtoupper($day . " " . $match[3] . " " . $mon . " " . $match[4]);
I'd like to propose a slightly different solution based on the not as oftenly used strptime. It uses a pre-defined format to parse the string.
Example:
<?php
// Specify a default timezone just in case one isn't set in php.ini.
date_default_timezone_set('America/Vancouver');
$str = "Tuesday, February 21 at 7:30am at Plano B";
if ($time = strptime($str, '%A, %B %e at %l:%M%P')) {
// This will default to the current year.
echo strtoupper(date('D d M H:i', mktime($time['tm_hour'], $time['tm_min'], 0, $time['tm_mday'], $time['tm_mon'])));
}
Output:
SUN 01 SEP 07:30
// Strip the last at, if its not a time
if(!preg_match("/at [0-9]+:[0-9]+[ap]m$/", $str)) {
$str = preg_replace("/at [^0-9].*/","",$str);
}
// Then convert to time
$time = strtotime($str);
// Then output in specified format
echo strtoupper(date("D d M h:i", $time));

Convert PHP Date to Time

How can I covert this PHP date string:
Thu 19th Aug 2010 # 7:52PM
to this:
1282247549
Which is done by:
gmdate("D jS M Y # g:iA", $row['project_deadline'])
The timestamp is from the database with the stored time() function
You can parse that string into a UNIX timestamp with the strtotime function:
$str = 'Thu 19th Aug 2010 # 7:52PM';
$str = str_replace('#', '', $str);
$timestamp = strtotime($str);
If you have unstandard format, then you should use DateTime::createFromFormat :
$str = 'Thu 19th Aug 2010 # 7:52PM';
$dt = DateTime::createFromFormat('!D jS M Y # g:iA', $str);
echo $dt->getTimestamp();

Categories