一天的枝条约会

时间:2015-04-19 10:41:45

标签: php symfony date twig

使用Symfony2,我的日期从我的控制器到我的树枝模板一天下车:

来自控制器的var_dump(在php中)给我正确的日期19-04-2015 从树枝模板转储给我18-04-2015。它让我发疯了!

控制器代码

public function displayAction($id)
{
    $product = new Product();
    $product = $this->getDoctrine()
            ->getRepository('PurchaseBundle:Product')
            ->find($id);

     foreach($product->getContracts() as $key=>$contract) {
             var_dump($contract);
         }

    $user = $this->get('security.context')->getToken()->getUser();
    if (!$this->get('security.context')->getToken()->getUser()->canReadProvider($product->getProvider())) {
        throw new AccessDeniedException();
    }

    $canWrite = $this->get('security.context')->getToken()->getUser()->canWriteProvider($product->getProvider());

    return $this->render('PurchaseBundle:Product:display.html.twig', array(
        'product'=>$product,
        'contracts'=>$product->getContracts(),
        'year'=>date('Y'),
        'canWrite'=>$canWrite));
}

Twig模板代码

    {% for contract in contracts %}
    <tr>
        <td colspan="5" class="table-2-content">
            {{ dump(contract) }}
            {% if contract.beginning and contract.end %}
            {{contract.beginning|date('d/m/Y')}} au {{contract.end|date('d/m/Y')}}
            {% else %}Date invalide{% endif %}
            <br />

我的合约实体有两个开始和结束日期,开始是正确的,结束是在树枝上比在PHP中更年轻一天......

此外,我还有另一个页面来修改这些日期,在这一个日期中两个日期都是正确的。

我在旧版本的Symfony 2.0上使用,Twig也老了,php也是5.3,我无法真正更新所有这些。

产品实体:

<?php

namespace XXX\PurchaseBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * @ORM\Entity(repositoryClass="XXX\PurchaseBundle\Repository\ProductRepository")
 * @ORM\Table(name="product")
 */
class Product
{
    /**
     * @var integer $id
     * 
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string $projectName
     * 
     * @ORM\Column(name="project_name", type="string", length=255, nullable=true)
     */
    protected $projectName;

    /**
     * @var string $commercialName
     * 
     * @ORM\Column(name="commercial_name", type="string", length=255, nullable=true)
     */
    protected $commercialName;

    /**
     * @ORM\ManyToOne(targetEntity="ProductSort", inversedBy="products")
     * @ORM\JoinColumn(name="product_sort_id", referencedColumnName="id")
     */
    protected $sort;

    /**
     * @var decimal $quotationValue
     *
     * @ORM\Column(name="quotation_value", type="decimal", scale=2, nullable=true)
     */
    protected $quotationValue;

    /**
     * @var string $comment
     * 
     * @ORM\Column(type="string", length=100, nullable=true)
     */
    protected $comment;

    /**
     * @var date $quotationDate
     * 
     * @ORM\Column(name="quotation_date", type="date", nullable=true)
     */
    protected $quotationDate;

    /**
     * @var date $ttm
     *
     * @ORM\Column(name="ttm", type="date", nullable=true)
     */
    protected $ttm;

    /**
     * @ORM\ManyToOne(targetEntity="Provider", inversedBy="products")
     * @ORM\JoinColumn(name="provider_id", referencedColumnName="id")
     * @Assert\Type(type="XXX\PurchaseBundle\Entity\Provider")
     */
    protected $provider;

    /**
     * @ORM\OneToMany(targetEntity="Contract", mappedBy="product",cascade={"remove"})
     * @ORM\OrderBy({"beginning" = "DESC"})
     */
    protected $contracts;

    /**
     * @ORM\OneToMany(targetEntity="Article", mappedBy="product",cascade={"remove"})
     */
    protected $articles;

    /**
     * @ORM\OneToMany(targetEntity="ProductFeature", mappedBy="product")
     */
    protected $productFeatures;

    /**
     * @ORM\ManyToMany(targetEntity="Classification", inversedBy="products")
     * @ORM\JoinTable(name="products_classifications")
     * @ORM\OrderBy({"year" = "DESC"})
     */
    protected $classifications;

    /**
     * @var date $updated
     *
     * @ORM\Column(name="updated", type="date", nullable=true)
     */
    private $updated;


    public function __construct() {
        $this->classifications = new ArrayCollection();
        $this->productFeatures = new ArrayCollection();
        $this->contracts = new ArrayCollection();
        $this->articles = new ArrayCollection();
        $this->features = new ArrayCollection();
    }

    public function __toString()
    {
        return $this->getProjectName();
    }


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set projectName
     *
     * @param string $projectName
     */
    public function setProjectName($projectName)
    {
        $this->projectName = $projectName;
    }

    /**
     * Get projectName
     *
     * @return string 
     */
    public function getProjectName()
    {
        return $this->projectName;
    }

    /**
     * Set commercialName
     *
     * @param string $commercialName
     */
    public function setCommercialName($commercialName)
    {
        $this->commercialName = $commercialName;
    }

    /**
     * Get commercialName
     *
     * @return string 
     */
    public function getCommercialName()
    {
        return $this->commercialName;
    }

    /**
     * Set quotationValue
     *
     * @param decimal $quotationValue
     */
    public function setQuotationValue($quotationValue)
    {
        $this->quotationValue = $quotationValue;
    }

    /**
     * Get quotationValue
     *
     * @return decimal 
     */
    public function getQuotationValue()
    {
        return (float) $this->quotationValue;
    }

    /**
     * Set comment
     *
     * @param string $comment
     */
    public function setComment($comment)
    {
        $this->comment = $comment;
    }

    /**
     * Get comment
     *
     * @return string 
     */
    public function getComment()
    {
        return $this->comment;
    }

    /**
     * Set quotationDate
     *
     * @param date $quotationDate
     */
    public function setQuotationDate($quotationDate)
    {
        $this->quotationDate = $quotationDate;
    }

    /**
     * Get quotationDate
     *
     * @return date 
     */
    public function getQuotationDate()
    {
        return $this->quotationDate;
    }

    /**
     * Set provider
     *
     * @param XXX\PurchaseBundle\Entity\Provider $provider
     */
    public function setProvider(\XXX\PurchaseBundle\Entity\Provider $provider)
    {
        $this->provider = $provider;
    }

    /**
     * Get provider
     *
     * @return XXX\PurchaseBundle\Entity\Provider 
     */
    public function getProvider()
    {
        return $this->provider;
    }

    /**
     * Add classifications
     *
     * @param XXX\PurchaseBundle\Entity\Classification $classifications
     */
    public function addClassification(\XXX\PurchaseBundle\Entity\Classification $classifications)
    {
        $this->classifications[] = $classifications;
    }

    /**
     * Remove classification
     *
     * @param XXX\PurchaseBundle\Entity\Classification $classification
     */
    public function removeClassification(\XXX\PurchaseBundle\Entity\Classification $classification)
    {
        $this->classifications->removeElement($classification);
    }

    /**
     * Get classifications
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getClassifications()
    {
        return $this->classifications;
    }

    /**
     * Get classification budget corresponding to a specific period
     *
     * @param $period String Period in "Qn YYYY" format (ex. "Q1 2012")
     * @return XXX\PurchaseBundle\Entity\Classification 
     */
    public function getClassificationBudgetByPeriod($period)
    {
        if(!$period || strlen($period) != 7)
            return '';

        $array = explode(' ', $period);

        foreach($this->getClassifications() as $classification) {
            if($classification->getYear() == intval($array[1])) {
                if($array[0] == 'Q1')
                    return $classification->getBudgetQ1();
                if($array[0] == 'Q2')
                    return $classification->getBudgetQ2();
                if($array[0] == 'Q3')
                    return $classification->getBudgetQ3();
                if($array[0] == 'Q4')
                    return $classification->getBudgetQ4();
            }
        }

        return '';
    }

    /**
     * Add contracts
     *
     * @param XXX\PurchaseBundle\Entity\Contract $contracts
     */
    public function addContract(\XXX\PurchaseBundle\Entity\Contract $contracts)
    {
        $this->contracts[] = $contracts;
    }

    /**
     * Get contracts
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getContracts()
    {
        return $this->contracts;
    }

    public function getContractAt($period)
    {
        $s = $period[0];
        $e = $period[1];
        foreach($this->contracts as $contract) {
            if(($contract->getEnd() >= $e and $contract->getBeginning() <= $e)
             or ($contract->getEnd() < $e and $contract->getEnd() > $s)) {
                return $contract;
            }
        }

        return null;
    }

    /**
     * Add articles
     *
     * @param XXX\PurchaseBundle\Entity\Article $articles
     */
    public function addArticle(\XXX\PurchaseBundle\Entity\Article $articles)
    {
        $this->articles[] = $articles;
    }

    /**
     * Get articles
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getArticles()
    {
        return $this->articles;
    }

    /**
     * Add productFeatures
     *
     * @param XXX\PurchaseBundle\Entity\ProductFeature $productFeatures
     */
    public function addProductFeature(\XXX\PurchaseBundle\Entity\ProductFeature $productFeatures)
    {
        $this->productFeatures[] = $productFeatures;
    }

    /**
     * Get productFeatures
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getProductFeatures()
    {
        return $this->productFeatures;
    }

    /**
     * Get productFeatures
     *
     * @return Doctrine\Common\Collections\Collection
     */
    public function getProductFeatureByCode($code)
    {
        foreach($this->productFeatures as $pf) {
            if($pf->getFeature()->getCode() == $code) {
                return $pf;
            }
        }
       return false;
    }

    /**
     * Set type
     *
     * @param string $type
     */
    public function setType($type)
    {
        $this->type = $type;
    }

    /**
     * Get type
     *
     * @return string 
     */
    public function getType()
    {
        return $this->type;
    }


    /**
     * Set sort
     *
     * @param XXX\PurchaseBundle\Entity\ProductSort $sort
     */
    public function setSort(\XXX\PurchaseBundle\Entity\ProductSort $sort)
    {
        $this->sort = $sort;
    }

    /**
     * Get sort
     *
     * @return XXX\PurchaseBundle\Entity\ProductSort 
     */
    public function getSort()
    {
        return $this->sort;
    }

    /**
     * Set updated
     *
     * @param date $updated
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;
    }

    /**
     * Get updated
     *
     * @return date 
     */
    public function getUpdated()
    {
        return $this->updated;
    }

    /**
     * Set ttm
     *
     * @param date $ttm
     */
    public function setTtm($ttm)
    {
        $this->ttm = $ttm;
    }

    /**
     * Get ttm
     *
     * @return date 
     */
    public function getTtm()
    {
        return $this->ttm;
    }
}

合同实体:

<?php

namespace XXX\PurchaseBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * XXX\PurchaseBundle\Entity\Contract
 *
 * @ORM\Table(name="contract")
 * @ORM\Entity(repositoryClass="XXX\PurchaseBundle\Repository\ContractRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Contract
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var decimal $billPrice
     *
     * @ORM\Column(name="bill_price", type="decimal", nullable=true, scale=3)
     */
    private $billPrice;

    /**
     * @var decimal $supplierWeekPrice
     *
     * @ORM\Column(name="supplier_week_price", type="decimal", nullable=true, scale=3)
     */
    private $supplierWeekPrice;

    /**
     * @var decimal $upchargeSav
     *
     * @ORM\Column(name="upcharge_sav", type="decimal", nullable=true, scale=3)
     */
    private $upchargeSav;

    /**
     * @var decimal $privateCopyingLevyValue
     *
     * @ORM\Column(name="private_copying_levy_value", type="decimal", nullable=true, scale=3)
     */
    private $privateCopyingLevyValue;

    /**
     * @var decimal $d3e
     *
     * @ORM\Column(name="d3e", type="decimal", nullable=true, scale=3)
     */
    private $d3e;

    /**
     * @var decimal $boatDiscount
     *
     * @ORM\Column(name="boat_discount", type="decimal", nullable=true, scale=3)
     */
    private $boatDiscount;

    /**
     * @var decimal $ecotaxe
     *
     * @ORM\Column(name="ecotaxe", type="decimal", nullable=true, scale=3)
     */
    private $ecotaxe;

    /**
     * @var decimal $srelec
     *
     * @ORM\Column(name="srelec", type="decimal", nullable=true, scale=3)
     */
    private $srelec;

    /**
     * @var decimal $otherTaxes
     *
     * @ORM\Column(name="other_taxes", type="decimal", nullable=true, scale=3)
     */
    private $otherTaxes;

    /**
     * @var \datetime $beginning
     *
     * @ORM\Column(name="beginning", type="date", nullable=true)
     */
    private $beginning;

    /**
     * @var \datetime $end
     *
     * @ORM\Column(name="end", type="date", nullable=true)
     */
    private $end;


    /**
     * @var XXX\PurchaseBundle\Entity\Product $product
     * 
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="contracts")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     */
    protected $product;

    /**
     * @var XXX\PurchaseBundle\Entity\MarketingFundRebate $marketingFundRebate
     *
     * @ORM\OneToOne(targetEntity="MarketingFundRebate",cascade={"all"})
     * @ORM\JoinColumn(name="marketing_fund_id", referencedColumnName="id")
     */
    protected $marketingFundRebate;

    /**
     * @var XXX\PurchaseBundle\Entity\XXXSecurisedRebate $XXXSecurisedRebate
     *
     * @ORM\OneToOne(targetEntity="XXXSecurisedRebate",cascade={"all"})
     * @ORM\JoinColumn(name="XXX_securised_rebate_id", referencedColumnName="id")
     */
    protected $XXXSecurisedRebate;

    /**
     * @var XXX\PurchaseBundle\Entity\XXXConditionalRebate $XXXConditionalRebate
     *
     * @ORM\OneToOne(targetEntity="XXXConditionalRebate",cascade={"all"})
     * @ORM\JoinColumn(name="XXX_conditional_rebate_id", referencedColumnName="id")
     */
    protected $XXXConditionalRebate;

    /**
     * @var XXX\PurchaseBundle\Entity\VodafoneSecurisedRebate $VodafoneSecurisedRebate
     *
     * @ORM\OneToOne(targetEntity="VodafoneSecurisedRebate",cascade={"all"})
     * @ORM\JoinColumn(name="vodafone_securised_rebate_id", referencedColumnName="id")
     */
    protected $vodafoneSecurisedRebate;

    /**
     * @var XXX\PurchaseBundle\Entity\VodafoneConditionalRebate $VodafoneConditionalRebate
     *
     * @ORM\OneToOne(targetEntity="VodafoneConditionalRebate",cascade={"all"})
     * @ORM\JoinColumn(name="vodafone_conditional_rebate_id", referencedColumnName="id")
     */
    protected $vodafoneConditionalRebate;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set supplierWeekPrice
     *
     * @param decimal $supplierWeekPrice
     */
    public function setSupplierWeekPrice($supplierWeekPrice)
    {
        $this->supplierWeekPrice = $supplierWeekPrice;
    }

    /**
     * Get supplierWeekPrice
     *
     * @return decimal 
     */
    public function getSupplierWeekPrice()
    {
        return (float) $this->supplierWeekPrice;
    }

    /**
     * Set upchargeSav
     *
     * @param decimal $upchargeSav
     */
    public function setUpchargeSav($upchargeSav)
    {
        $this->upchargeSav = $upchargeSav;
    }

    /**
     * Get upchargeSav
     *
     * @return decimal 
     */
    public function getUpchargeSav()
    {
        return (float) $this->upchargeSav;
    }

    /**
     * Set privateCopyingLevyValue
     *
     * @param decimal $privateCopyingLevyValue
     */
    public function setPrivateCopyingLevyValue($privateCopyingLevyValue)
    {
        $this->privateCopyingLevyValue = $privateCopyingLevyValue;
    }

    /**
     * Get privateCopyingLevyValue
     *
     * @return decimal 
     */
    public function getPrivateCopyingLevyValue()
    {
        return (float) $this->privateCopyingLevyValue;
    }

    /**
     * Set d3e
     *
     * @param decimal $d3e
     */
    public function setD3e($d3e)
    {
        $this->d3e = $d3e;
    }

    /**
     * Get d3e
     *
     * @return decimal 
     */
    public function getD3e()
    {
        return (float) $this->d3e;
    }

    /**
     * Set boatDiscount
     *
     * @param decimal $boatDiscount
     */
    public function setBoatDiscount($boatDiscount)
    {
        $this->boatDiscount = $boatDiscount;
    }

    /**
     * Get boatDiscount
     *
     * @return decimal 
     */
    public function getBoatDiscount()
    {
        return (float) $this->boatDiscount;
    }

    /**
     * Set otherTaxes
     *
     * @param decimal $otherTaxes
     */
    public function setOtherTaxes($otherTaxes)
    {
        $this->otherTaxes = $otherTaxes;
    }

    /**
     * Get otherTaxes
     *
     * @return decimal 
     */
    public function getOtherTaxes()
    {
        return (float) $this->otherTaxes;
    }

    /**
     * Set ecotaxe
     *
     * @param decimal $ecotaxe
     */
    public function setEcotaxe($ecotaxe)
    {
        $this->ecotaxe = $ecotaxe;
    }

    /**
     * Get ecotaxe
     *
     * @return decimal 
     */
    public function getEcotaxe()
    {
        return (float) $this->ecotaxe;
    }

    /**
     * Set srelec
     *
     * @param decimal $srelec
     */
    public function setScotaxe($srelec)
    {
        $this->srelec = $srelec;
    }

    /**
     * Get srelec
     *
     * @return decimal 
     */
    public function getSrelec()
    {
        return (float) $this->srelec;
    }

    /**
     * Set beginning
     *
     * @param date $beginning
     */
    public function setBeginning($beginning)
    {
        $this->beginning = $beginning;
    }

    /**
     * Get beginning
     *
     * @return date 
     */
    public function getBeginning()
    {
        return $this->beginning;
    }

    /**
     * Set end
     *
     * @param date $end
     */
    public function setEnd($end)
    {
        $this->end = $end;
    }

    /**
     * Get end
     *
     * @return date 
     */
    public function getEnd()
    {
        return $this->end;
    }

    /**
     * Set product
     *
     * @param XXX\PurchaseBundle\Entity\Product $product
     */
    public function setProduct(\XXX\PurchaseBundle\Entity\Product $product)
    {
        $this->product = $product;
    }

    /**
     * Get product
     *
     * @return XXX\PurchaseBundle\Entity\Product
     */
    public function getProduct()
    {
        return $this->product;
    }

    /**
     * Set billPrice
     *
     * @param decimal $billPrice
     */
    public function setBillPrice($billPrice)
    {
        $this->billPrice = $billPrice;
    }

    /**
     * Get billPrice
     *
     * @return decimal 
     */
    public function getBillPrice()
    {
        return (float) $this->billPrice;
    }


    /**
     * Set marketingFundRebate
     *
     * @param XXX\PurchaseBundle\Entity\MarketingFundRebate $marketingFundRebate
     */
    public function setMarketingFundRebate($marketingFundRebate)
    {
        $this->marketingFundRebate = $marketingFundRebate;
    }

    /**
     * Get marketingFundRebate
     *
     * @return XXX\PurchaseBundle\Entity\MarketingFundRebate 
     */
    public function getMarketingFundRebate()
    {
        return $this->marketingFundRebate;
    }

    /**
     * Set XXXSecurisedRebate
     *
     * @param XXX\PurchaseBundle\Entity\XXXSecurisedRebate $XXXSecurisedRebate
     */
    public function setXXXSecurisedRebate($XXXSecurisedRebate)
    {
        $this->XXXSecurisedRebate = $XXXSecurisedRebate;
    }

    /**
     * Get XXXSecurisedRebate
     *
     * @return XXX\PurchaseBundle\Entity\XXXSecurisedRebate 
     */
    public function getXXXSecurisedRebate()
    {
        return $this->XXXSecurisedRebate;
    }

    /**
     * Set XXXConditionalRebate
     *
     * @param XXX\PurchaseBundle\Entity\XXXConditionalRebate $XXXConditionalRebate
     */
    public function setXXXConditionalRebate($XXXConditionalRebate)
    {
        $this->XXXConditionalRebate = $XXXConditionalRebate;
    }

    /**
     * Get XXXConditionalRebate
     *
     * @return XXX\PurchaseBundle\Entity\XXXConditionalRebate 
     */
    public function getXXXConditionalRebate()
    {
        return $this->XXXConditionalRebate;
    }

    /**
     * Set VodafoneSecurisedRebate
     *
     * @param XXX\PurchaseBundle\Entity\VodafoneSecurisedRebate $vodafoneSecurisedRebate
     */
    public function setVodafoneSecurisedRebate($vodafoneSecurisedRebate)
    {
        $this->vodafoneSecurisedRebate = $vodafoneSecurisedRebate;
    }

    /**
     * Get VodafoneSecurisedRebate
     *
     * @return XXX\PurchaseBundle\Entity\VodafoneSecurisedRebate 
     */
    public function getVodafoneSecurisedRebate()
    {
        return $this->vodafoneSecurisedRebate;
    }

    /**
     * Set VodafoneConditionalRebate
     *
     * @param XXX\PurchaseBundle\Entity\VodafoneConditionalRebate $vodafoneConditionalRebate
     */
    public function setVodafoneConditionalRebate($vodafoneConditionalRebate)
    {
        $this->vodafoneConditionalRebate = $vodafoneConditionalRebate;
    }

    /**
     * Get VodafoneConditionalRebate
     *
     * @return XXX\PurchaseBundle\Entity\VodafoneConditionalRebate 
     */
    public function getVodafoneConditionalRebate()
    {
        return $this->vodafoneConditionalRebate;
    }

    /**
     * Set srelec
     *
     * @param decimal $srelec
     */
    public function setSrelec($srelec)
    {
        $this->srelec = $srelec;
    }
}

1 个答案:

答案 0 :(得分:4)

twig documentation

  

如果日期已经是DateTime对象,并且如果要保留其当前时区,则将false作为时区值传递:

使用此代码:

{{contract.beginning|date('d/m/Y', false)}}