Given the following PHP code:
$thestring = "Saturday - 05/10/2014 at 10:00 am";
echo $thestring."\n";
$thestring = str_replace("at ", "", $thestring);
echo $thestring."\n";
$thestring = substr($thestring, strpos($thestring, " - ")+3);
echo $thestring."\n";
$thedate = date_create_from_format("m/d/Y h:m a", $thestring);
echo $thedate->format("Ymd")."\n";
echo $thedate->format("Y")."\n";
echo $thedate->format("m")."\n";
echo $thedate->format("d")."\n";
Why am I getting the output below? The month and year are off and I don't see a logical explanation for it.
Saturday - 05/10/2014 at 10:00 am
Saturday - 05/10/2014 10:00 am
05/10/2014 10:00 am
20131210
2013
12
10
m is the modifier for months. i is the modifier for minutes. Getting this wrong will obviously cause issues. Change:
$thedate = date_create_from_format("m/d/Y h:m a", $thestring);
to:
$thedate = date_create_from_format("m/d/Y h:i a", $thestring);
Related
While trying:
echo "<br />".$opening_time = "02 May 2019 - 03:10";
echo "<br />".$closing_time = "12 May 2019 - 13:40";
echo "<br />".$string_opening_time = strtotime($opening_time);
echo "<br />".$string_closing_time = strtotime($closing_time);
echo "<br />".$diffrence_time = $string_closing_time - $string_opening_time;
The results are:
02 May 2019 - 03:10 12
May 2019 - 13:40
//2 blank lines
0
Why it is blank when I convert it to strtotime?
The format d M Y - H:i is not recognized, but you can recreate it as a DateTime object if you know what format it will be in using DateTime::createFromFormat().
Create two DateTime objects and use the diff() method on them, that will give you the difference.
$opening_time = "02 May 2019 - 03:10";
$closing_time = "12 May 2019 - 13:40";
$open = DateTime::createFromFormat("d M Y - H:i", $opening_time);
$close = DateTime::createFromFormat("d M Y - H:i", $closing_time );
$diff = $open->diff($close);
echo $opening_time."<br />\n";
echo $closing_time."<br />\n";
echo $diff->d." days ".$diff->h." hours ".$diff->m." minutes ";
Live demo at https://3v4l.org/3pKr4
If you need the difference in seconds, use the getTimestamp() methods.
$open = DateTime::createFromFormat("d M Y - H:i", $opening_time);
$close = DateTime::createFromFormat("d M Y - H:i", $closing_time );
$diff = $close->getTimestamp() - $open->getTimestamp();
echo $opening_time."<br />\n";
echo $closing_time."<br />\n";
echo $diff;
Live demo at https://3v4l.org/ZG5LJ
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
I am having some trouble trying to convert string to time.
My Code is:
$time = strtotime("14 November, 2013 2:30 AM");
echo $time ."<br />";
echo date("m/d/Y", $time);
I know that strtotime is not magic, and I checked out the acceptable date/time formats but I am not sure how to convert the string to another string without converting it to time first.
What's the easiest way to accomplish this?
Take a look at DateTime::createFromFormat and then call format on the created DateTime instance.
Something like:
$yourTimeString = '14 November, 2013 2:30 AM';
$date = DateTime::createFromFormat('d F, Y h:i A', $yourTimeString);
echo $date->format('m/d/Y');
One way is to rewrite the string using explode and list.
<?php
// here we assume "day month, year time AMPM"
$date = "14 November, 2013 2:30 AM";
// assign a variable to each part of the string
list($day,$month,$year,$time,$ampm) = explode(" ",$date);
// remove the commas at the end of the month
$month = str_replace(',','',$month);
// Now we rewrite the strtotime string
$time = strtotime($month . " " . $day . ", " . $year . " " . $time . " " . $ampm);
echo $time ."<br />";
echo date("m/d/Y", $time);
Php date() function allow natural language string for parsing date,
for Ex:
echo date("d-M-Y", strtotime("first monday of 2019-07")); // returns first monday of july 2019
echo date("d-M-Y", strtotime("last sat of July 2008"));
You can find here php instructions to parsing date as natural languages.
http://php.net/manual/en/datetime.formats.relative.php
I have this PHP code:
$end=date('Y-m-d');
I use it to get the current date, and I need the date 5 years in the future, something like:
$end=date('(Y + 5)-m-d');
How can I do this?
Try with:
$end = date('Y-m-d', strtotime('+5 years'));
Modifying dates based on this post
strtotime() is really powerful and allows you to modify/transform dates easily with it’s relative expressions too:
Procedural
$dateString = '2011-05-01 09:22:34';
$t = strtotime($dateString);
$t2 = strtotime('-3 days', $t);
echo date('r', $t2) . PHP_EOL; // returns: Thu, 28 Apr 2011 09:22:34 +0100
DateTime
$dateString = '2011-05-01 09:22:34';
$dt = new DateTime($dateString);
$dt->modify('-3 days');
echo $dt->format('r') . PHP_EOL; // returns: Thu, 28 Apr 2011 09:22:34 +0100
The stuff you can throw at strtotime() is quite surprising and very human readable. Have a look at this example looking for Tuesday next week.
Procedural
$t = strtotime("Tuesday next week");
echo date('r', $t) . PHP_EOL; // returns: Tue, 10 May 2011 00:00:00 +0100
DateTime
$dt = new DateTime("Tuesday next week");
echo $dt->format('r') . PHP_EOL; // returns: Tue, 10 May 2011 00:00:00 +0100
Note that these examples above are being returned relative to the time now.
The full list of time formats that strtotime() and the DateTime constructor takes are listed on the PHP Supported Date and Time Formats page.
Another example, suitable for your case could be: based on this post
<?php
//How to get the day 3 days from now:
$today = date("j");
$thisMonth = date("n");
$thisYear = date("Y");
echo date("F j Y", mktime(0,0,0, $thisMonth, $today+3, $thisYear));
//1 week from now:
list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
echo date("F j Y", mktime(0,0,0, $thisMonth, $today+7, $thisYear));
//4 months from now:
list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
echo date("F j Y", mktime(0,0,0, $thisMonth+4, $today, $thisYear));
//3 years, 2 months and 35 days from now:
list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
echo date("F j Y", mktime(0,0,0, $thisMonth+2, $today+35, $thisYear+3));
?>
Use this code to add years or months or days or hours or minutes or seconds to a given date
echo date("Y-m-d H:i:s", strtotime("+1 years", strtotime('2014-05-22 10:35:10'))); //2015-05-22 10:35:10
echo date("Y-m-d H:i:s", strtotime("+1 months", strtotime('2014-05-22 10:35:10')));//2014-06-22 10:35:10
echo date("Y-m-d H:i:s", strtotime("+1 days", strtotime('2014-05-22 10:35:10')));//2014-05-23 10:35:10
echo date("Y-m-d H:i:s", strtotime("+1 hours", strtotime('2014-05-22 10:35:10')));//2014-05-22 11:35:10
echo date("Y-m-d H:i:s", strtotime("+1 minutes", strtotime('2014-05-22 10:35:10')));//2014-05-22 10:36:10
echo date("Y-m-d H:i:s", strtotime("+1 seconds", strtotime('2014-05-22 10:35:10')));//2014-05-22 10:35:11
You can also subtract replacing + to -
$date = strtotime($row['timestamp']);
$newdate = date('d-m-Y',strtotime("+1 year",$date));
Its very very easy with Carbon.
$date = "2016-02-16"; // Or Your date
$newDate = Carbon::createFromFormat('Y-m-d', $date)->addYear(1);
Using Carbon:
$dt = Carbon::now();
echo $dt->addYears(5);
To add one year to todays date use the following:
$oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", mktime()) . " + 365 day"));
You may use DateInterval for this purpose;
$currentDate = new \DateTime(); //creates today timestamp
$currentDate->add(new \DateInterval('P5Y')); //this means 5 Years
and you can now format it;
$currentDate->format('Y-m-d');
Try below code, i hope it will be helpful for you
<?php
$current_date=strtotime(date('Y-m-d'));
echo $end = date('Y-m-d', strtotime('+5 years',$current_date));
?>
try this ,
$presentyear = '2013-08-16 12:00:00';
$nextyear = date("M d,Y",mktime(0, 0, 0, date("m",strtotime($presentyear )), date("d",strtotime($presentyear )), date("Y",strtotime($presentyear ))+5));
echo $nextyear;
try this:
$yearnow= date("Y");
$yearnext=$yearnow+1;
echo date("Y")."-".$yearnext;
Try this code and add next Days, Months and Years
// current month: Aug 2018
$n = 2;
for ($i = 0; $i <= $n; $i++){
$d = strtotime("$i days");
$x = strtotime("$i month");
$y = strtotime("$i year");
echo "Dates : ".$dates = date('d M Y', "+$d days");
echo "<br>";
echo "Months : ".$months = date('M Y', "+$x months");
echo '<br>';
echo "Years : ".$years = date('Y', "+$y years");
echo '<br>';
}
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));