Convert month numbers to letters PHP - php

I have this code to echo some records from a mysql database:
<?php
$host = "localhost";
$gebruikersnaam = "Admin";
$wachtwoord = "Test123";
mysql_connect($host, $gebruikersnaam, $wachtwoord);
$database = "olf";
mysql_select_db($database);
$result = mysql_query("SELECT * FROM agenda WHERE datum ORDER BY datum ASC LIMIT 0, 3");
while($row = mysql_fetch_array($result))
{
?>
<ul class="dates">
<li>
<span class="date"><?php echo "" .substr($row['datum'],5,2). "";?></strong></span>
</li>
<?php } ?>
</ul>
In the database the format of the 'datum' (date) is YYYY-MM-DD, I echo just the part MM. So this is the format: 01 (January), 02 (February) etc. What I want:
If " .substr($row['datum'],5,2). " is equal to 01, then convert it to JAN
If " .substr($row['datum'],5,2). " is equal to 02, then convert it to FEB
If " .substr($row['datum'],5,2). " is equal to 03, then convert it to MAR
ETC..
How do I need to make this?

You shouldn't have to hassle with this yourself - let MySQL do the heavy lifting for you:
SELECT DATE_FORMAT (datum, '%b') AS month_abbreviation
FROM agenda

Use the date function to display the date in a specific format.
In this case, M displays the three first letters of the months name.
echo date("M", strtotime($row['datum']));

Convert the incoming information to a DateTime object, then you can apply all sorts of fancy formatting rules:
$date = \DateTime::createFromFormat( "Y-m-d", $row['datum'] );
echo $date->format( 'M' ); // for just month in text
echo $date->format( 'D dS of F Y' ); // for example, "Monday 6th of November 2014"
Check out the workings of \DateTime on the php website:
http://nl1.php.net/manual/en/datetime.createfromformat.php

Related

converting a string to date in php

I have a string that contains date and time. Format of my string is yyyymmddtime.
For example. 20171125123000209. this is my complete string in which first comes the month, then month and then day after that time. How can i retrieve date from it by converting to readable date format. I tried with php's date function. But the output was not as expected. Please help.
Try this
<?php
$str_date= "20171125123000209";
$exiting_date_format='Ymd';
//first 8 characters from given date string in second parameter below
$date = DateTime::createFromFormat($exiting_date_format, substr($str_date,0,8));
echo $date->format('Y-m-d');//specify desired date format
?>
Output :
2017-11-25
DateTime::createFromFormat - Parses a time string according to a specified format
You can use date() function of php
<?php
$full_date= "20171125123000209";
echo date('dS F h:i:s A', $full_date);
//Output = 31st May 12:30:09 AM
?>
Or,
To get date from timestamp like now,
$timestamp= time(); //Or your timestamp here
$date = date('d-m-Y', $timestamp); //Inside first parameter, give your date format
echo $date; //17-12-2017
Or,
To get anything from string you can also use substr() function of php.
<?php
$full_date= "20171125123000209";
$year = substr($full_date, 0, 4);
$month = substr($full_date, 4, 2);
$date = substr($full_date, 6, 2);
echo 'Year = '.$year.' ';
echo 'Month = '.$month.' ';
echo 'Date = '. $date.' ';
?>
Output:
Year = 2017 Month = 11 Date = 25
Test in jdoodle
About substr() function in php
It's not necessary to manipulate the string at all. PHP's DateTime class supports parsing a string containing miliseconds natively, using the u format modifier:
$str = '20171125123000209';
$date = DateTime::createFromFormat('YmdHisu', $str);
Using your new $date object, you can convert to whatever format you're looking for, e.g.
echo $date->format("F j Y, g:i a");
// November 25 2017, 12:30 pm
See https://eval.in/920636
$your_strtotime_val = ""; //20171125123000209
$convert_to_date = date("m-d-Y h:i:s",$your_strtotime); // [m-d-Y h:i:s] this depending on how you convert date in first time so be careful

Select date mysql request

I have the request below in a php file. I'm trying to get a date in this format: MONTHS-currentMonth-currentYear
$sql = "SELECT DAY(ADDDATE(`dateDebutC`, `dureeC`)) AS MONTHS FROM normalW WHERE id = '$id'";
If you want to format the date after returning the values from your sql query, then you can use the PHP date_format() function. Assuming you store the months value in a variable, let's call it $date:
echo date_format($date,'Y-m-d'); // Or whatever format you want to display
To add the current month and current year to that string, you can use the date() function:
$current_month = date(m); // prints current month in numeric format (01-12)
$current_year = date(Y); // prints current year as a 4-digit representation
$new_date = $date . '-' . $current_month . '-' . $current_year;
See https://www.w3schools.com/php/func_date_date.asp for a list of all parameters. And as a friendly reminder, please ensure that you are using mysqli rather than mysql, which is deprecated.
To get current month and year you can use GETDATE function in sql
$sql = "SELECT MONTH(ADDDATE(`dateDebutC`, `dureeC`))AS MONTHS,MONTH(GETDATE()) as curmonth,YEAR(GETDATE()) as curyear FROM normalW where id = '$id'";
Whatever this comes through as in result, for example, however you bind it:
$date = '2014-04-15 10:10:11';
$buildDate = new DateTime(strtotime($date));
$dowMonthDayYear = $buildDate->format('l F j, Y');
echo dowMonthDayYear // turns as April 15th, 2014
You can lookup datetime in php manual for other formats theres tons

Time and date echo from mysql table

I have this question:
I have selected date & time from the table & echoed them just fine but I don't like the date & time format that is echoed. Here in Central-Southern Africa, we are used to 24hrs (like 16:30hrs instead of 4:30pm etc).
Here is the code:
("SELECT * FROM me order by 1 DESC LIMIT 0,10");
$date = $run_post['date'];
$time = $run_post['time'];
And them I do this:
echo $date;
and it gives me 2015-11-13 but I want 13th November 2015
and then
echo $time;
giving me 2015-11-13 12:53:43 but want like 16:30:00 hrs format.
Finally, I also want to echo my (UTC+02:00) Cairo Time zone. Currently it is giving me -2hrs
You should use the DateTime Class as you can set the timezone in it.
Example with Timezone Europe/London:
$date = new DateTime('2015-11-13 12:53:43', new DateTimeZone('Europe/London'));
echo $date->format('d\t\h F Y') . "\n";
echo $date->format('H:i:s') . 'hrs';
How to add Timezone to DateTime
I'm assuming that your default time zone is set to UTC. If you want to display time as per your time zone(UTC+02:00), then you can do something like this:
function display_time($time){
$unixdatetime = strtotime($time) + 7200;
return strftime("%d %B %Y, %H:%M %p",$unixdatetime);
}
And when you call this display_time function, for example,
echo display_time($run_post['time']);
then it would display,
13 November 2015, 14:53 PM
format method of DateTime class is what you need.
$date = new DateTime($run_post['time']);
echo $date->format('d\t\h\,F Y');
Will echo something like 13th, November 2015)
You have the documentation of how to format in
https://secure.php.net/manual/en/function.date.php

Formating an Oracle timestamp with PHP

I want to separate Date and time from retrieved timestamp in oracle with php but it's not working. I tried to separate date and working after that i have to separate time but i am getting empty timestamp.
$db = $row['CREATEDATE']; // CREATEDATE=03-SEP-15 12.55.17.000000 PM
echo 'db...'.(string)$db."<br/>";
$timestamp = strtotime((string)$db);
echo 'timestamp...'.$timestamp."<br/>";
echo date("m-d-Y", $timestamp);
Convert your received column data to a string, and convert it to a datetime. Then you can do all other formatting on it.
$db = $row['CREATEDATE']; //$db need to be converted to string type
$datetime = new DateTime((string)$db); //I expect (string)$db will considered as a string
$date = $datetime->format('Y M d'); //convert to date
$time = $datetime->format('H:i:s'); //convert to time
$select_stmt = "select to_char(ENDTIME,' hh24:mi:ss') AS CHENG,to_char(RESERVETIME,' hh24:mi:ss') AS CHENG2 FROM RESERVATION WHERE RESERVATIONSTATUS='RENTING' AND RESERVATIONFACILITY = '".$_SESSION['cno']."'";
$stid = oci_parse($con, $select_stmt);
oci_execute($stid);
while ($row = oci_fetch_assoc($stid))
{
$datt=$row["CHENG"];
$datt2=$row["CHENG2"];
}

Format dynamic datetime

I apologize if this has been asked, I've just filtered through about 40 questions on this site and other sites about formatting dynamic datetime and have only seen answers for date() etc. The issue is that I am trying to format registration dates and last login dates for a member based system that currently is formatted as such: 2012-09-29 10:21:40 to Sep 2012but as this is dynamic data I am not sure how to do this. This information is pulled from the database as such, just for the sake of clarification:
$sql = mysql_query("SELECT * FROM members WHERE id='$id'");
while($row = mysql_fetch_array($sql)){
$username = $row['username']; // Ignore
$sign_up = $row["sign_up"];
$last_login = $row["last_login"];
}
As date() doc says:
date('M Y', strtotime($row["sign_up"]));
If you want to do it in PHP you must reformat the datetime from mysql youself:
$sign_up = date('M Y', strtototime($row["sign_up"]));
$last_login = date('M Y', strtototime($row["last_login"]));
If I understood it correctly, you wish to convert the DATETIME format to be displayed in PHP as something like Sep 2012. You may wish to look at date and strtotime too in the PHP Manual.
$sql = mysql_query("SELECT * FROM members WHERE id='$id'");
while($row = mysql_fetch_array($sql)){
$username = $row['username']; // Ignore
$sign_up = $row["sign_up"];
$last_login = $row["last_login"];
$convert = strtotime($sign_up);
$convert = date('M Y',$convert);
echo $convert . '<br />'; // this should be in the format Sep 2012
}

Categories