Convert 3 variables to a date in php/laravel 5.1 [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
Im trying to convert 3 variables $day $month $year. Into a date here is my code
$user->age = 23;
$day = array_rand($days);
$month = array_rand($months);
$year = date('Y', strtotime('-'.$user->age.' years'));
$date_combine = $day.$month.$year;
$convert = strtotime($date_combine);
$dob = date('d/M/Y', $convert);
dd($dob);
when I output the $dob I just get "01/Jan/1970" when I should be getting "01/Jan/1993". Not sure why this I happening or what Im missing.
Note: Im using laravel 5.1.

Try using Carbon (it´s used by Laravel) like this:
$dob = Carbon::createFromDate($year, $month, $day);
I hope it could help you.

I hope this may help you
$user->age = 23;
$time = strtotime("-$user->age year", time());
echo $date = date("Y-m-d", $time);
Here time() is used for getting current time.

Related

How to add one day to a date which is given as 'month-day'? [duplicate]

This question already has answers here:
Adding one day to a date
(13 answers)
Closed 5 years ago.
I have a date which is:
$firstDay = "'" . date('m-d', easter_date(date('Y'))) . "'";
Output of $firstDay is: '04-16'. Now I need to create a second variable and add 1 day to the $firstDay variable. How should I do that? I red that I can do with $date->modify(), I'm trying like so:
$secondDay = $firstEasterDay->modify('+1 day');
But this doesn't work and I got an error.
Thanks for any help.
<?php
$date = "04-15-2013";
$date = strtotime($date);
$date = strtotime("+1 day", $date);
echo date('m-d-Y', $date);?>
Try This

How to extract the values of a date in y-m-d format? [duplicate]

This question already has answers here:
Split string from a date
(6 answers)
Closed 6 years ago.
Let's suppose I have this date: 2016-07-27. What I want to achieve is:
$year = 2016;
$month = 07;
$day = 27;
What I have tried:
$year = preg_match("/^[^-]*/", "2016-07-27");It returns: 1
$month = preg_match("(?<=\-)(.*?)(?=\-)", "2016-07-27");It returns: Warning: preg_match(): Unknown modifier '('
$year = ???
How can I extract the numbers between the dashes and store them into the variables below as shown above?
Don't reinvent the wheel - date_parse will do all the heavy lifting for you:
$parsed = date_parse('2016-07-27');
$year = $parsed['year'];
$month = $parsed['month'];
$day = $parsed['day'];
If it's a string that wont change its format, then you can simply do this:
$date = '2016-07-27';
list($year, $month, $day) = explode('-', $date);
echo $year; // 2016
echo $month; // 07
echo $day; // 27
However, if the date format changes, then you should use date_parse or other DateTime methods.

php - date reset to 1970 at 2038 while adding in a loop [duplicate]

This question already has answers here:
PHP & mySQL: Year 2038 Bug: What is it? How to solve it?
(7 answers)
Closed 6 years ago.
I have been trying to generate a list of years and months based on some integer value provided by user in my PHP based web app. While doing this I faced the year 2038 bug, the year got reset to 1970 when the loop reached to 2038. The PHP version I am using is 5.4 and I googled and applied the solution found in this link, but it is not working for me, did anyone try some other solution for the 2038 bug in php?
Here is my PHP code:
$i = 1;
$Tenurein = 360;
while ($i < $Tenurein) {
$MonthArr[] = date('F', strtotime($date2));
$YearArr[] = date('Y', strtotime($date2));
$date = new DateTime($date2);
$date->add(new DateInterval('P1M'));
$dateMon = $date->format('Y-m-d');
$date2 = $dateMon;
$i++;
}
.
I'm using php version 5.6. Below code producing output
View output
<?php
$i = 1;
$Tenurein = 360;
$date2 = date('Y-m-d');
while ($i < $Tenurein) {
$MonthArr[] = date('F', strtotime($date2));
$YearArr[] = date('Y', strtotime($date2));
$date = new DateTime($date2);
$date->modify('+1 month');
echo $dateMon = $date->format('Y-m-d');
$date2 = $dateMon;
$i++;
echo "<br>";
}
?>
We could see lot of posts regarding this issue. For displaying date it seems there are no issues. But while storing in database i too faced same issue. if you use timestamp data type just change as datetime. It will work.

PHP: Date Formatting - MM/DD/YY > YYYY/MM/DD [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
Hi I am trying to get some PHP date formatting resolved. The problem I am having is random unexpected results being returned instead of the correct date after I try and format them.
I have the date MM/DD/YY passed in as a string and I am wondering how to convert that to a YYYY/MM/DD. When it try to convert the date it is like it is staying in the same place but trying to convert the individual section so the MM (12) goes to the year YYYY (2012)
Here is the section of code I have been using to try and change the format:
$date = $_GET["datepicker"];
$arr[$i] = strftime("%Y-%m-%d", strtotime($date));
The $arr[$i] is just the array I am putting it into this shouldn't affect anything.
I have also tried the following:
$arr[$i] = date("Y-m-d", strtotime($date));
Thanks,
Kieran
The easiest way to get the output you need is to use the DateTime class, in particular: DateTime::createFromFormat:
$date = DateTime::createFromFormat('m/j/y', $_GET['datepicker']);
//now to get the outpu:
$arr[$i] = $date->format('Y-m-d');
There is a procedural-style alternative, too:
$date = date_create_from_format('m/j/y', $_GET['datepicker']);
//note, date_create_from_format returns an instance of DateTime
$arr[$i] = date_format($date, 'Y-m-d');//same as $date->format('Y-m-d');
You can use mktime:
$date = $_GET["datepicker"];
list($month, $day, $year) = explode('/', $date);
$timestamp = mktime(0, 0, 0, $month, $day, $year);
$output = date('Y-m-d', $timestamp)

Format date using php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a PHP variable '$date', and it's value is: y/m/d h:i:s format.
e.g: $date = "2014-11-10 17:25:00";
I want to change this format to m/d/y h:i:s format, so that I'm using PHP code below. It's working fine but is there any PHP function to do it in one line of code ?
$date = "2014-11-10 17:25:00";
$explode = explode(" ", $date);
$part1 = $explode[0];
$part2 = $explode[1];
$explode = explode("-", $part1);
$year = $explode[0];
$month = $explode[1];
$day = $explode[2];
$created_date = "$month-$day-$year $part2";
You can use date() & strtotime() functions. Example:
$date = "2014-11-10 17:25:00";
echo date('m-d-Y H:i:s', strtotime($date));
Output:
11-10-2014 17:25:00

Categories