PHP separate variable container [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I have a variable that contains a date
$date = "04/18/2017 04:02 PM";
This comes from a date field, This field will always contain a date like this, and What I'm trying to do is separating the date from the time
So how do i go about getting only 04/18/2017 from the variable or the time 04:02 PM ?
Thanks.

Just do a simple explode:
$explode = explode($date, ' ', 2);
$date = $explode[0];
$time = $explode[1];

Related

Get day number from Date format by php [duplicate]

This question already has answers here:
How to extract only 'Day' value from full 'Date' string?
(7 answers)
Closed 2 years ago.
I have a date Formate from database number 2020-11-15 I want to get only the day number. I tried the following.
$your_date = '2020-11-15';
$ar_day2 = date("D", strtotime($your_date)); //The value is (Sun)
I am looking for result '15'.
Is there way by php?
use small d
$ar_day2 = date("d", strtotime('2020-11-15'));
echo $ar_day2 = date("d", strtotime('2020-11-15')); // will give => 15
echo $ar_day2 = date("D", strtotime('2020-11-15')); // will give => Sun

How to convert string "290416" in date format in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I need to convert string value "290416" which is actually date but not in correct format. I need to change it in date format like 29/04/16.
please help.
If you don't need it as a date but only in date format. Meaning you are not performing any date-functions on it but just displaying it as a date you could use
$str = '290416';
$arr = str_split($str, 2);
$date_string = $implode('/', $arr);
The most robust way will be to use createFromFormat, passing in your format and the string, and they you have a DateTime object and can do many things with it.
define('MY_DATE_INPUT_FORMAT', 'mdy');
define('MY_DATE_OUTPUT_FORMAT', 'm/d/y');
$inputDateString = '042916';
$dateObj = DateTime::createFromFormat(MY_DATE_INPUT_FORMAT, $inputDateString);
$outputString = $dateObj->format(MY_DATE_OUTPUT_FORMAT);
This can also be done procedurally:
$date = date_create_from_format(MY_DATE_INPUT_FORMAT, $inputDateString);
echo date_format($date, MY_DATE_OUTPUT_FORMAT);

Convert date to M-d-y format in PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
How do I change the date format using PHP? I have used the following code but it is displaying the date input field value as m-d-Y. Why it is changing the format?
$effectivedate = date('M-d-Y', strtotime($empCompensationdata-effective_date));
$effective_date = date('Y-m-d', strtotime($empCompensationdata-effective_date));
$("#effective_date").val($effectivedate);
$('#effective_date').attr('data-value', $effective_date);
The reason the value is displayed as M-d-Y is because that's what you're assigning to it with the val() method:
$effectivedate = date('M-d-Y', strtotime($empCompensationdata->effective_date));
$effective_date = date('Y-m-d', strtotime($empCompensationdata->effective_date));
$("#effective_date").val($effectivedate); //This variable contains "M-d-Y"
$('#effective_date').attr('data-value', $effective_date);
So if you want to have Y-m-d, just change the assigning variable:
$("#effective_date").val($effective_date); //This variable contains "Y-m-d"

Php get month number from string with date [duplicate]

This question already has answers here:
Get month of a given date
(6 answers)
Closed 7 years ago.
i have a string in this format '2015-4-28'
How i get month number for this string ( the value in this example is 4)?
Thanks
Another option would be to do this:
date('m', strtotime($date));
Change the m to an n to get the month without the leading 0.
With this:
<?php
$a = "2015-4-28";
$parts= explode("-", $a);
echo $parts[0]; // 2015
echo $parts[1]; // 4
echo $parts[2]; // 28
?>

Take PHP variable value of 201501 for example and display January 2015 [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a date value that is being passed as 201501.
How could I take that and display January 2015 instead?
It's being passed through a PHP variable.
Just try with:
$input = '201501';
$output = DateTime::createFromFormat('Ym', $input)->format('F Y');
Try below code
$data = '201501';
$monthNum = substr($data,4);
$year = substr($data,0,4);
$dateObj = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F');
echo $monthName.' '.$year;

Categories