Hello I have this date date format ("Y-m-d") I want to separate them in parts and print only my month of that date. How do I separate it?
date_default_timezone_set('America/Mexico_City');
$fecha=date("Y-m-d ");
Hello I have this date date format ("Y-m-d") I want to separate them in parts and print only my month of that date. How do I separate it?
date_default_timezone_set('America/Mexico_City');
$fecha=date("Y-m-d ");
I would recommend that you work using Object Oriented Programming, using the class DateTime
.
If in $fecha
you receive a date like this for example: 2018-11-15
:
$objFecha = new DateTime($fecha, new DateTimeZone('America/Mexico_City'));
$mes= $objFecha->format('m');
echo $mes;
The output there would be 11
.
@Esther
The solution that you comment here can be improved. There is no need to dizzy the data, making explode
with them.
$mostrarso=mysql_query($so);
while($i=mysql_fetch_array($mostrarso)){
$fecha_mes=$i[1];
$partes = explode('-', $fecha_mes);
$_fecha = "{$partes[2]}-{$partes[1]}-{$partes[0]}";
<td><?php echo $partes[1];?> </td>
}
If you learn to work with objects you will save yourself a lot of headaches and many lines of code. For example:
while($i=mysql_fetch_array($mostrarso)){
$fecha=new DateTime($i[1]);
<td><?php echo $fecha->format('m');?> </td>
}
And, if you want to show the name of the month in Spanish, you can use this utilitarian class , because with PHP per se, it is not possible to do it (if you are not interested in the whole class, adapt the month conversion function and nothing else or bring the array of months that the class uses and convert in situ ):
/*
*Dado que sería una clase propia debes incluirla
*aquí suponemos que la clase está en un archivo llamado fecha_es.php
*en el directorio classes
*/
include('classes/fecha_es.php');
while($i=mysql_fetch_array($mostrarso)){
$fecha=new FechaEs($i[1]);
<td><?php echo $fecha->getMMMM();?> </td>
}
If you want to get the month, from a field type: Y-m-d
that comes from your database I propose the following scenario
$fecha = "2018-11-25";
echo date('m', strtotime($fecha));
Where $fecha
simulates being a value of type DATE
that comes from my database; later with the function date()
of php I pass as the first parameter the value I want to obtain in quotes, in this case it is the month, later use the function strtotime()
to which I pass the name of the variable that contains the date that I want to manipulate
It should be clarified that:
M
minpuscula I get the number of the month The function m
serves and I quote according to php.net
(PHP 4, PHP 5, PHP 7)
strtotime - Convert a textual date / time description into English to a Unix date
Link to official documentation: link
So if there are several data you can do the following
SELECT column1, column2, etc MONTH(columnaFecha) AS Mes FROM tu tabla;
You use the function strtotime()
to which inside the parentheses you pass the name of the column that has the date; that way they arrive formatted direct of the DB
You can do this
date_default_timezone_set('America/Mexico_City');
$month = date("m");
$year = date("y");
For what you are going to occupy, only, concatenate it with .
Try this ...
$fecha = $_POST['fecha'];
date_default_timezone_set('America/Mexico_City');
$fecha=date("Y-m-d ");
$month = date("m");
$year = date("y");
$day = date("d");
$partes = explode('-', $month);
$_fecha = "$year.$month.$day";
<td><?php echo $month;?> </td>