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
Related
I have a query that inserts date in this format
$time = date("m-d-y");
But when I Fetch and output date it shows wrong date. real: October 2020 but outputs: January 1970
$time = $row1['time'];
$newDate = date('F Y', strtotime($time));
echo $newDate;
How do I output date in this format: 20 OCT, 2020
Try not to store date/time using varchar datatype. If you can change it, please do. However, if you can't change database structure, you can use date_create_from_format() to create a DateTime object from a custom format:
echo date_create_from_format('m-d-y', "10-29-20")->format('F Y');
Output:
October 2020
Edit: Change the format part to ->format('d M, Y') to match your desired format
Output:
29 Oct, 2020
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
i want to convert a date format to another using php
6th of November 2017 to 2017-11-6
i tried
$newDate = date("Y-m-d", strtotime('6th of November 2017'));
but it always returns 01-01-1970
When you create date from specific format then use createFromFormat of date.
$date = DateTime::createFromFormat('jS \o\f F Y', '6th of November 2017');
echo $date->format('Y-m-d');
Note: If you have string in your format then you have to escape that string (with \) for conversation
DEMO
Replace 6th of to 6th and it will be fine..
$newDate = date("Y-m-d", strtotime('6th November 2017'));
echo ($newDate);
result
2017-11-06
Try this:
$newDate = date("Y-m-d", strtotime("6 November 2017"));
Would be grateful for any assistance with this.
I have:
$value = gmdate("F d Y", getlastmod());
This is returning (for example):-
June 24 2016
My question is how to extract the dayofweek information out of $value?
My output needs to look like:
Friday, June 24, 2016
Is there a way to extract the dayofweek, Month, dayofmonth and year out of $value? If not, how can I get those values from 'getlastmod' ?
Many thanks
First you will need to use strtotime to convert your current $value to timestamp.
Then by using the date()function you can get the day of the week.
Example:
<?php
echo date("l", strtotime("June 24 2016")); // Output: Friday
Try this instead:
$value = gmdate("l, F d, Y", getlastmod());
Output:
Friday, June 24, 2016
$myDate = date_create(getlastmod());
echo $myDate->format('L, F d, Y');
This will print Friday, June 24, 2016
More on date/time formats here:
http://php.net/manual/en/function.date.php
From the PHP formatting guide for date (gmdate is the same as date except it is using GMT) you can use "l" to get the full text of the day of the week.
http://php.net/manual/en/function.date.php
So in your example I think the following should work to meet your output needs.
$value=gmdate("l, F d, Y",getlastmod());
This question already has answers here:
Add number of days to a date
(20 answers)
Closed 6 years ago.
Hello all i am trying to add 30 days to my date. I am using below coding.
<?php
$next_due_date = date('05/06/2016', strtotime("+30 days"));
echo $next_due_date;
?>
But it is returning to "05/06/2016" only!
Please help me!
Do not use php's date() function, it's not as accurate as the below solution and furthermore it is unreliable in the future.
Use the DateTime class
<?php
$date = new DateTime('2016-06-06'); // Y-m-d
$date->add(new DateInterval('P30D'));
echo $date->format('Y-m-d') . "\n";
?>
The reason you should avoid anything to do with UNIX timestamps (time(), date(), strtotime() etc) is that they will inevitably break in the year 2038 due to integer limitations.
The maximum value of an integer is 2147483647 which converts to Tuesday, 19 January 2038 03:14:07 so come this time; this minute; this second; everything breaks
Source
Another example of why I stick to using DateTime is that it's actually able to calculate months correctly regardless of what the current date is:
$now = strtotime('31 December 2019');
for ($i = 1; $i <= 6; $i++) {
echo date('d M y', strtotime('-' . $i .' month', $now)) . PHP_EOL;
}
You'd get the following sequence of dates:
31 December
31 November
31 October
31 September
31 August
31 July
31 June
PHP conveniently recognises that three of these dates are illegal and converts them into its best guess, leaving you with:
01 Dec 19
31 Oct 19
01 Oct 19
31 Aug 19
31 Jul 19
01 Jul 19
Please try this.
echo date('m/d/Y',strtotime('+30 days',strtotime('05/06/2016'))) . PHP_EOL;
This will return 06/06/2016. Am assuming your initial date was in m/d/Y format. If not, fret not and use this.
echo date('d/m/Y',strtotime('+30 days',strtotime(str_replace('/', '-', '05/06/2016')))) . PHP_EOL;
This will give you the date in d/m/Y format while also assuming your initial date was in d/m/Y format. Returns 05/07/2016
If the input date is going to be in mysql, you can perform this function on mysql directly, like this.
DATE_ADD(due_date, INTERVAL 1 MONTH);
The first parameter is the format, not the current date.
<?php
$next_due_date = date('d/m/Y', strtotime("+30 days"));
echo $next_due_date;
Demo: https://eval.in/583697
If you want to add the 30 days to a particular starting date use the second parameter of the strtotime function to tell it where to start.
When in doubt about how a function works refer to the manual.
http://php.net/manual/en/function.strtotime.phphttp://php.net/manual/en/function.date.php
You need to provide date format like d/m/Y instead of 05/06/2016
Try
$old_date = '05-06-2016';
$next_due_date = date('d-m-Y', strtotime($old_date. ' +30 days'));
echo $next_due_date;
$date = "1998-08-14";
$newdate = strtotime ( '30 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate;
Found the above code
This question already has answers here:
Using strtotime for dates before 1970
(7 answers)
Closed 9 years ago.
I am trying to separate time from the given string date & time. I simply tried with following example.
$time = '2014-01-20 10:45:45';
echo '<br>'.$finalTime = date("H:i:s",strtotime($time));
echo '<br>'.$date = date("d F Y",strtotime($time));
I am getting correct date and time.
10:45:45
20 January 2014
But when I tried with given string, no correct result.
$time = '1899-12-30 19:30:00';
echo '<br>'.$finalTime = date("H:i:s",strtotime($time));
echo '<br>'.$date = date("d F Y",strtotime($time));
PHP is always returning me following result.
00:00:00
01 January 1970
I am not sure whether is there any limitation on date function that is not returning 1899. Is that so?
Your date is before the unix epoch. DateTime() allows you to work around that.
$dt = new DateTime("1899-12-30 19:30:00");
echo $dt->format("d F Y");
echo $dt->format("h:i:s");
Use DateTime and DateTime::format():
$time = '1899-12-30 19:30:00';
$dt = new DateTime($time);
echo $dt->format('d F Y H:i:s');
Working example: http://3v4l.org/fM22Z
The strtotime() method is limited by the Unix epoch, which is Jan 1, 1970.
Update: As Mark comments below, your code would work all the way back to 1901 (as a negative timestamp), see here: http://3v4l.org/CSJte