How can I convert date from 8 to AUG - php

I would like to convert the number to actual month. I only need a month.
I am now trying like this
$str = 8;
$anInt = intval($str);
$test = date('M',$anInt);
echo $test;
the result is JAN, I was supposed to get "AUG" as a result. I dont know why.
any idea or suggestion for me?

The date() function assumes a timestamp.
You should try this:
$test = date('M',mktime(0,0,0,$anInt,0,0);
From the docs for date and mktime:
// Prints: Your date's month is : August
echo "Your date's month is : " . date("F", mktime(0, 0, 0, $anInt, 0, 0));

Use mktime.
echo date('M', mktime(0, 0, 0, $str));

Here you are:
echo date('M',strtotime('08/01/2012'));
Or, if you want all caps:
echo strtoupper(date('M',strtotime('08/01/2012')));
There might be other approaches to this, but this was the first to come to mind

Duplicate question. Read here.
Read about date function here.
using date() you should use timestamp.
To convert date value to timestamp, use strtotime
$date = '2012-8-8';
$timestamp = strtotime($date);
echo date('M',$timestamp);
For your problem:
$monthnumber = 4;
$date = '2012-'.$monthnumber.'-8';
$timestamp = strtotime($date);
echo date('M',$timestamp);

Related

convert the month number to date

I am working on a task where I need the user to provide exact number of months it will take them to complete a task and then i need to convert that number to an exact date, so lets suppose a user enters 6, this should give me a date 6 months from now.
I tried the following code looking at different examples on line but I have a feeling the following examples treats the $monthNum as the actual month of a year rather than what I need it to do.
$monthNum = 5;
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
echo $monthName;
I will really appreciate any assistance here.
Demo here
Pop in your month in the modify() method.
$monthNum = 6;
$date = new DateTime();
$date->modify(" +{$monthNum} month");
echo $date->format("Y-m-d");
Outputs
2015-05-14
You can try:
$time = new \DateTime('+5 months');
You can use strtotime:
$date = date("Y-m-d", strtotime("+5 months"));
in php 5.4+
echo (new DateTime())->modify('+6 months')->format('d M Y');

How to find Day from date string in PHP?

i have string like below "June 1, 2012". i need to find day for the above string.
this value return from wordpress postdate() function.
how can i do this in php?
Can't you parse the date with strtotime?
If so, you can do it like this:
<?php
date('l', strtotime('June 1, 2012'));
?>
<?php echo date('d',strtotime('June 1, 2012')); ?>
You can use a variable instead of the static date string.
$unix_timestamp = strtotime($date);
echo date('l', $unix_timestamp); // displays Monday|Tuesday|Wednesday...
$date = "June 1, 2012";
echo date('l', strtotime($date));
$timestamp= strtotime("June 1, 2012");
$day = date('l', $timestamp);
echo $day;
For more details, read strtotime and date
$date = "June 1, 2012"
$day = date('j', strtotime($date))
You can do different options in the date function to display the day (or other date parts as needed)
Although the answer is easily found, you would be best to research first:
http://uk.php.net/manual/en/function.strtotime.php

convert a date in this format '031012' into a date that I can add days to

I have a date in this format 030512 (ddmmyy).
But I'm having trouble with converting this to a date usable format I can add days to.
Basically.. I extracted the date above from a text file, Now I need to be able to add a number of days to it. But I am having trouble parsing the date in this format.
Is there another way of doing this rather then something like this:
// I have a date in this format
$date = '030512'; // 03 May 2012
$day = substr($date,0,2);
$month = substr($date, 2,2);
$year = substr($date, 4,2);
$date_after = $day . "-" . $month . "-".$year;
// Now i need to add x days to this
$total_no_nights = 010; // must be this format
$days_to_add = ltrim($total_no_nights,"0"); // 10, remove leading zero
// how do i add 10 days to this date.
You can do this (php >= 5.3):
$date = DateTime::createFromFormat('dmy', '030512');
$date->modify('+1 day');
echo $date->format('Y-m-d');
http://www.php.net/manual/en/datetime.createfromformat.php
For php < 5.3 :
$dateArray = str_split('030512', 2);
$dateArray[2] += 2000;
echo date("d/m/Y", strtotime('+1 day', strtotime(implode('-', array_reverse($dateArray)))));
try this using the month/day/year you already have:
$date = "$month/$day/$year";
$change = '+10 day';
echo date("d/m/Y", strtotime($change, strtotime($date)));
Assuming the date will always be in the future (or at least after 1st Jan 2000), you're not far wrong:
// I have a date in this format
$date = '030512'; // 03 May 2012
$day = substr($date,0,2);
$month = substr($date, 2,2);
$year = substr($date, 4,2);
// dd-mm-yy is not a universal format but we can use mktime which also gives us a timestamp to use for manipulation
$date_after = mktime( 0, 0, 0, $month, $day, $year );
// Now i need to add x days to this
$total_no_nights = "010"; // must be this format
$days_to_add = intval( $total_no_nights ); // No need to use ltrim
// Here's the "magic". Again it returns a timestamp
$new_date = strtotime( "+$days_to_add days", $date_after );
Using the DateTime object would be easier but you say you're not on PHP5.3.
You can't do date manipulation with strings becase, well, they are not dates. In PHP, you can use Unix timestamps (which are actually integers...) or DateTime objects. In you case:
$timestamp = strtotime('-10 days', mktime(0, 0, 0, $month, $day, $year));
echo date('r', $timestamp);
... or:
$object = new DateTime("$year-$month-$day");
$object->modify('-10 days');
echo $object->format('r');
Also, please note that 010 is an octal number that corresponds to 8 in decimal.
using the convert function in sql the date can be obtained in appropriate format.
anter that operations can be performed in php to manipulate the date. i hope that answers your query.

How to convert the date into paticular format using PHP?

I am having the input box for getting the value in mm-dd-yyyy format.So When I get the date I try to covert it into YYYY-MM-DD format.But It doesnt work.
For Example :
<?
$birthdate="08-13-2000";
$date=date('Y-m-d',strtotime($birthdate));
echo $date;
?>.
Output is 1970-01-01.
But If I gave 13-08-2000 I got 2000-08-13.I dont want to split.Because in my application I used in manyplaces like this.But I think the strtotime convert it into unix timestamp format even whatever the value.That's why I am try to do like.What's wrong I am understanding or doing?Thanks in advance.
strtotime() parses dash-separated dates as dd-mm-yyyy. You'll need to input birthdate as "08/13/2000". str_replace should do the job for that if you can't change the expected seperator for the input.
credit for the separator differences to sam at frontiermedia dot net dot au from php.net
Edit: Have some sample code for if you need to do the replace:
$birthdate = '08-13-2000';
$birthdate = str_replace('-','/',$birthdate);
$date = date('Y-m-d',strtotime($birthdate));
echo $date;
Otherwise it'd just be
$birthdate = '08/13/2000';
... snip ...
Try this:
$date = explode("-", "08-13-2000"); // input MM-DD-YYYY
$time = mktime(0, 0, 0, $date[0], $date[1], $date[2]); // to time
$date = date('Y-m-d', $time); // transform to Y-m-d
echo $date; // output YYYY-MM-DD
$date[0] is the month
$date[1] is the day
$date[2] is the year
And if you use it in manyplaces, make a function():
function transformDate($input)
{
$date = explode("-", $input);
$time = mktime(0, 0, 0, $date[0], $date[1], $date[2]);
$date = date('Y-m-d', $time);
return $date;
}
echo transformDate("08-13-2000");

php - mktime or strtotime?

I'm trying to convert 2010-02 to February, 2010. But, I keep getting December, 1969
I've tried using mktime, strtotime, and some combination of the two, but still haven't been able to do it...
This is what I tried most recently...
$path_title = date('F, Y', mktime(0,0,0,2,0,2010));
This would be a way to do it:
$dateString = '2010-02';
list($year, $month) = explode('-', $dateString);
$timeStamp = mktime(0, 0, 0, $month, 1, $year);
echo date('F, Y', $timestamp);
Another way would be:
$dateString = '2010-02';
$timestamp = strtotime($dateString . '-01');
echo date('F, Y', $timestamp);
strtotime can't handle ambiguous dates like "2010-02", but if you make it a full date it should work.
Otherwise, you may want to look into something like DateTime::createFromFormat.
Try this:
$str = '2010-02';
echo date('F, Y',mktime(0,0,0,substr($str,-2),1,substr($str,0,4)));
You have to make sure you use valid values to mktime(). In your example that you edited into the question, you have 0 as the day, which is effectively the first day minus one, which puts you into the previous month.

Categories