I attempted this:
$date_string = strtotime('6 Mar, 2011 23:59:59');
But I think PHP can't interpret that for some reason as it returned empty. I tried this:
$date_string = strtotime('6 Mar, 2011 midnight');
The above worked but I need it to be a second before midnight i.e. the last second of the day. How can I get strtotime to return this without changing the 6 Mar, 2011 part?
Hope this helps. I used this and it gives todays timestamp just before midnight. Counter intuitive.
$today_timestamp = strtotime('tomorrow - 1 second');
It works for me if I use March 6, 2011 23:59:59. Any chance of changing the input format?
Other than that, you could of course subtract 1 second from the timestamp. Note however that you need to use March 7:
$date_string = strtotime('7 Mar, 2011 midnight') - 1;
Why not use mktime?
mktime(23,59,59,3,6,2011);
If you're on PHP 5.3 or greater, you could use the DateTime class.
The createFromFormat function allows you to manually specify how to parse your input date string.
$date = '6 Mar, 2011 23:59:59';
$timestamp = DateTime::createFromFormat('d M, Y H:i:s', $date)->getTimestamp();
Related
I have a date string that contains the month and the day: "January 20"
I need to convert this to a timestamp in PHP and can use strtotime to do so. The catch is I always want to return a future timestamp. So if it's December 30, 2020 today, and I have the string "January 20," I want to return the timestamp for January 20, 2021 -- not January 20, 2020.
I've come up with a few convoluted ways of doing this, such as converting to a timestamp then making sure the timestamp is > the current time. If it's not, add a year and re-convert to a timestamp. But it seems like there may be a best practice and simple way of doing this. Any suggestions?
Thanks,
Tim
I'd try to compare current timestamp against this year timestamp. It should work.
$str = 'January 20';
$now = time();
$thisYear = strtotime($str);
$nextYear = strtotime($str . ' + 1 year');
$futureDate = $thisYear < $now ? $nextYear : $thisYear;
// 2021-01-20 - for January 20
// 2022-01-01 - for January 01
I'd like to use the datetime->modify function on a date string that's formatted like "21 Jan 2016". When I use the datetime->modify and add 1 day, it gives me a result of 30 Apr 2017. I know that if I don't use the short month name and use a number instead (i.e. 01), it will work fine but I would like to get it work this way with short month name. Is this possible?
Please see code below:
<?php
$date = "21 Jan 2016"; // this is my date string
$newdate = new DateTime($date );
$date2 = $newdate->modify('+1 day'); // add 1 day to date string
echo $date2->format("d-M-Y");
?>
RESULT is:
30-Apr-2017
RESULT WANTED
22-Jan-2016
The problem is that you are trying to create a DateTime object from a non-ISO format. That's that part that is not working.
Take a look at: http://php.net/manual/ro/datetime.createfromformat.php
You will need to have something like
DateTime::createFromFormat('d M Y', '21 Jan 2016');
Full example:
$tomorrow = DateTime::createFromFormat('d M Y', '21 Jan 2016')->modify('+1 day')->format("d-M-Y");
echo($tomorrow);
The format of the $date variable is incorrect. Off the top of my head, there are two easy ways to fix this:
Set $date = "Jan 21, 2016"
Set $date = "21-Jan 2016"
More options: https://secure.php.net/manual/en/datetime.formats.date.php
Your date format was wrong. That's all.
How can I format this date 01 August, 15 for mysql DATE field. So I need this 2015-08-01 format from 01 August, 15 this format in PHP. Tried following but not work
echo date('Y-m-d',strtotime('01 August, 15'));
It is because strtotime() does not understand what 01 August, 15 means. Try it:
var_dump(strtotime('01 August, 15')); // false
The 15 at the end is too ambiguous; it could be the day of the month or a short year.
The easiest way to make this work is probably to use DateTime::createFromFormat, like so:
$date = '01 August, 15';
$parsed = DateTime::createFromFormat('d F, y', $date);
echo $parsed->format('Y-m-d');
If you control the format of the date then you could also make it easier to parse. Formatting it like 01 August 2015 would work, for example.
First remove the , out of the date and then use the strtotime function.
So:
$date = "01 August, 15";
$date = str_replace(",", "", $date);
echo date("Y-m-d",strtotime($date));
hello guys i have date like this
25 March 2014 - 16:45
i want to convert this date in to this format how to do that please Help
2014-03-25 16:45:00
i try this $creation = date('Y-m-d H:i:s',strtotime('25 March 2014 - 16:45'));
but it's not work
i try this $creation = date('Y-m-d H:i:s',strtotime('25 March 2014 - 16:45')); but it's not work
The reason your code doesn't work is because '25 March 2014 - 16:45' is not in a format that strtotime() can parse.
strtottime() is good at handling a wide range of formats, but it can't cope with absolutely anything; there's just too much variation possible.
I suggest that you try PHP's DateTime class instead. This class has a method called createFromFormat() which allows you to specify the format of the incoming date string. This makes it easier for it to parse, and allows for formats that might not be recognised otherwise, like yours.
Try this:
$date = DateTime::createFromFormat('j F Y - H:i', '25 March 2014 - 16:45');
echo $date->format('Y-m-d H:i:s');
Use PHP DateTime
You have "-" in "25 March 2014 - 16:45" in the string so it not be able to read by DateTime
So work around would be
$str = "25 March 2014 -16:45";
$date = new DateTime(str_replace("-","",$str));
echo $date->format('Y-m-d H:i:s');
I've successfully tried the following with your string:
$a = strptime('25 March 2014 - 16:45', '%d %B %Y - %R');
$time = mktime(
$a["tm_hour"],
$a["tm_min"],
$a["tm_sec"],
$a["tm_mon"]+1,
$a["tm_mday"],
$a["tm_year"]+1900
);
$converted = date('Y-m-d H:i:s',$time);
Try this:
$creation = date('Y-m-d H:i:s',strtotime('25 March 2014 16:45'));
i was fetching this date from table in the database like this format
Sunday 16th of January 2011 06:55:41 PM
and i want to convert it to be like this format
11-05-2012
how to do that with date function or any function
when i use date function
<td><?php echo date('d-m-Y', $v['v_created']); ?></td>
i get error message
'Severity: Warning
Message: date() expects parameter 2 to be long, string given'
This works for me (just tested on local web server)
<?php
date_default_timezone_set ('Europe/Rome');
$date = "Sunday 16th of January 2011 06:55:41 PM";
//.Strip "of" messing with php strtotime
$date = str_replace('of', '', $date);
$sql_friendly_date = date('y-m-d H:i', strtotime($date));
echo $sql_friendly_date;
?>
You can format the date as you prefer changing the first parameter of Date function according to: http://it2.php.net/manual/en/function.date.php
You have the following format:
Sunday 16th of January 2011 06:55:41 PM
that is a string based format, so the date information is more or less encoded in a human readable format. Luckily in english language. Let's see, that are multiple things all separated by a space:
Sunday - Weekdayname
16th - Date of Month, numeric, st/nd/th form
of - The string "of".
January - Monthname
2011 - Year, 4 digits
06:55:41 - Hour 2 digits 12 hours; Colon; Minute 2 digits; Colon; Seconds 2 digits
PM - AM/PM
So you could separate each node by space and then analyze the data. All you need is all Monthnames and the sscanf function because you only need to have the month, date of month and year:
$input = 'Sunday 16th of January 2011 06:55:41 PM';
$r = sscanf($input, "%*s %d%*s of %s %d", $day, $monthname, $year);
Which will already give you the following variables:
$monthname - string(7) "January"
$day - int(16)
$year - int(2011)
So all left to do is to transpose the monthname to a number which can be done with a map (in the form of an array in PHP) and some formatted output:
$monthnames = array(
'January' => 1,
# ...
);
printf("%02d-%02d-%04d", $day, $monthnames[$monthname], $year);
So regardless of which problem, as long as the input is somewhat consistently formatted you can pull it apart, process the gained data and do the output according to your needs. That is how it works.
try this. always worked for me
$date = Sunday 16th of January 2011 06:55:41 PM
$new_date = date('d-M-Y', strtotime($date));
echo $new_date;
The format you are using Sunday 16th of January 2011 06:55:41 PM is a wrong format.from the form you are inserted this date in database should be in date(Y-m-d) than the value of date inserted in database like:- 11-05-2012. and you can fetch this and get the format what you want.
<?php
$old_date = date('l, F d y h:i:s'); // returns Saturday, January 30 10 02:06:34
$new_date = date('d-M-Y', strtotime($old_date));
echo $new_date
?>
more details about date plz visit this url
http://www.php.net/manual/en/datetime.createfromformat.php