Format date using php [duplicate] - php

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

Related

unable to get date and time from timestamp in php? [duplicate]

This question already has answers here:
Why don't PHP and Javascript's timestamps match?
(3 answers)
Closed 6 years ago.
I have written the following code:
$timestamp = "1483625713000";
$date = date('d-m-Y', $timestamp);
$time = date('Gi.s', $timestamp);
but the output is not coming as expected:
My outputs:
echo $date // 23-03-48984
echo $time // 2136.40
Looks like you've got milliseconds in that timestamp. Try this:
$timestamp = 1483625713000 / 1000;
$date = date('d-m-Y', $timestamp);
$time = date('Gi.s', $timestamp);
var_dump($date);
var_dump($time);
Outputs
string(10) "05-01-2017"
string(7) "1415.13"

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.

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

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.

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)

PHP - Convert mm/yyyy to mysql Date Format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
So far I'm doing this to convert dates
$date = '01/1990';
date('Y-m-d', strtotime(str_replace('-', '/', $date . '/01')));
But I get back a boolean false; Any ideas?
Your str_replace is searching for - and replacing with /.
Here is a simplified solution that opts to not concatenate the day onto the date string.
$date = '01/1990';
$date = str_replace('/', '-', $date);
$date = date('Y-m-1', strtotime($date));
If you do want to concatenate your day onto the date string format as dd/mm/yyyy
$date = '01/1990';
$date = '01/' . $date;
$date = str_replace('/', '-', $date);
$date = date('Y-m-d', strtotime($date));
Replace $data with $date:
echo date('Y-m-d', strtotime(date('Y-d-m', strtotime('01/' . str_replace('-', '/', $date)))));
strtotime accepts dd-mm-yyyy and mm/dd/yyyy (and some other formats like yyyy-mm-dd). The only way to differentiate between dd-mm-yyyy and mm/dd/yyyy is the separator.
Since you start with a string that appears to be in mm/yyyy format, the easiest solution would be to convert it to dd-mm-yyyy:
$date = '01/1990';
date('Y-m-d', strtotime('01-'.str_replace('/', '-', $date)));
And you also might want to check the DateTime class.

Categories